cygwin 控制台上没有错误或 cout 消息

发布于 2024-12-28 06:38:19 字数 5957 浏览 1 评论 0原文

我在 Windows7 机器上安装了 cygwin,并且正在运行一些 C++ 代码。当我运行此代码时:

#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
#include <math.h>
#include "Pop.h"
using namespace std;


int main() {
  ifstream in;
  ofstream out;
  cout << "something";
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "1\n";
  else
    cout << "unable to open res7 file";
  out.close();
  int seed = time(NULL);
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "2\n";
  else
    cout << "unable to open res7 file";
  out.close();
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "seed " << seed << '\n';
  else
    cout << "unable to open res7 file";
  out.close();
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "3\n";
  else
    cout << "unable to open res7 file";
  out.close();
  srand (seed);
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "4\n";
  else
    cout << "unable to open res7 file";
  out.close();
  double parameters[4];
  int location;
  in.open("input7.txt", ios::in);
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "5\n";
  else
    cout << "unable to open res7 file";
  out.close();
  return 0;
}

我在控制台上看到输出(某些内容)。但是当我运行完整的代码时:

#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
#include <math.h>
#include "Pop.h"
using namespace std;


int main() {
  ifstream in;
  ofstream out;
  cout << "something";
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "1\n";
  else
    cout << "unable to open res7 file";
  out.close();
  int seed = time(NULL);
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "2\n";
  else
    cout << "unable to open res7 file";
  out.close();
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "seed " << seed << '\n';
  else
    cout << "unable to open res7 file";
  out.close();
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "3\n";
  else
    cout << "unable to open res7 file";
  out.close();
  srand (seed);
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "4\n";
  else
    cout << "unable to open res7 file";
  out.close();
  double parameters[4];
  int location;
  in.open("input7.txt", ios::in);
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "5\n";
  else
    cout << "unable to open res7 file";
  out.close();
     if (in.is_open()) {
    out.open("res7.txt", ios::out|ios::app);
    if (out.is_open())
      out << "6\n";
    else
      cout << "unable to open res7 file";
    out.close();
    char line[256];
    while (!in.getline(line, 256).eof()) {
      out.open("res7.txt", ios::out|ios::app);
      if (out.is_open())
    out << "7\n";
      else
    cout << "unable to open res7 file";
      out.close();
      char *input = strtok(line, " ");
      int i=0;
      while (input!=NULL) {
    out.open("res7.txt", ios::out|ios::app);
    if (out.is_open())
      out << "8\n";
    else
      cout << "unable to open res7 file";
    out.close();
    parameters[i] = atof(input);
    input = strtok(NULL, " ");
    i++;
      }
      int rnum = rand();
      location = rnum%10000;
      out.open("res7.txt", ios::out|ios::app);
      if (out.is_open())
    out << "9\n";
      else
    cout << "unable to open res7 file";
      out.close();
      Pop p(parameters[0], parameters[1], 0.2, 0.1, parameters[2], location);
      out.open("res7.txt", ios::out|ios::app);
      if (out.is_open())
    out << "10\n";
      else
    cout << "unable to open res7 file";
      out.close();
      double L = exp(-parameters[3]);
      double FF_mean = p.FF_steady(parameters[3], L, 0.0);
      out.open("res7.txt", ios::out|ios::app);
      if (out.is_open())
    out << "11\n";
      else
    cout << "unable to open res7 file";
      out.close();
      out.open("res7.txt", ios::out|ios::app);
      if (out.is_open()) {
    out << "number of chromosomes is " << p.all_population[0]->ga->chromosome << "  alpha is " << parameters[2] << "  cost is " << parameters[1];
    out << "  a is " << parameters[0] << "  u is " << parameters[3] << "  location is " << location << "  FF steady is " << FF_mean << '\n';
      }
      else
    cout << "unable to open res7 file FF";
      out.close();
      out.open("res7.txt", ios::out|ios::app);
      if (out.is_open())
    out << "12\n";
      else
    cout << "unable to open res7 file";
      out.close();
      if (FF_mean>0.1) {
    out.open("res7.txt", ios::out|ios::app);
    if (out.is_open())
      out << "13\n";
    else
      cout << "unable to open res7 file";
    out.close();
    p.insert_O(0.05);
    out.open("res7.txt", ios::out|ios::app);
    if (out.is_open())
      out << "14\n";
    else
      cout << "unable to open res7 file";
    out.close();
    double total_mean = p.steady(parameters[3], L, 0.0);
    double perc = p.perc_O();
    out.open("res7.txt", ios::out|ios::app);
    if (out.is_open())
      out << "total steady is " << total_mean << "   perc O is " << perc << '\n';
    else
      cout << "unable to open res7 file";
    out.close();
      }
    }
  }
  else
    cout << "Unable to open input7 file\n";
    in.close();
  return 0;
}

我在控制台上看不到任何输出。当我在另一台机器上运行代码时,我发现出现浮点异常。我想我可以找到它在哪里,但我不明白的是为什么我在 cygwin 的控制台上看不到任何输出或错误。谁能帮助我吗?

I installed cygwin on my Windows7 machine, and I'm running some c++ code. When I'm running this code:

#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
#include <math.h>
#include "Pop.h"
using namespace std;


int main() {
  ifstream in;
  ofstream out;
  cout << "something";
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "1\n";
  else
    cout << "unable to open res7 file";
  out.close();
  int seed = time(NULL);
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "2\n";
  else
    cout << "unable to open res7 file";
  out.close();
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "seed " << seed << '\n';
  else
    cout << "unable to open res7 file";
  out.close();
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "3\n";
  else
    cout << "unable to open res7 file";
  out.close();
  srand (seed);
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "4\n";
  else
    cout << "unable to open res7 file";
  out.close();
  double parameters[4];
  int location;
  in.open("input7.txt", ios::in);
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "5\n";
  else
    cout << "unable to open res7 file";
  out.close();
  return 0;
}

I see an output on the console (something). But when I run my complete code:

#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
#include <math.h>
#include "Pop.h"
using namespace std;


int main() {
  ifstream in;
  ofstream out;
  cout << "something";
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "1\n";
  else
    cout << "unable to open res7 file";
  out.close();
  int seed = time(NULL);
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "2\n";
  else
    cout << "unable to open res7 file";
  out.close();
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "seed " << seed << '\n';
  else
    cout << "unable to open res7 file";
  out.close();
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "3\n";
  else
    cout << "unable to open res7 file";
  out.close();
  srand (seed);
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "4\n";
  else
    cout << "unable to open res7 file";
  out.close();
  double parameters[4];
  int location;
  in.open("input7.txt", ios::in);
  out.open("res7.txt", ios::out|ios::app);
  if (out.is_open())
    out << "5\n";
  else
    cout << "unable to open res7 file";
  out.close();
     if (in.is_open()) {
    out.open("res7.txt", ios::out|ios::app);
    if (out.is_open())
      out << "6\n";
    else
      cout << "unable to open res7 file";
    out.close();
    char line[256];
    while (!in.getline(line, 256).eof()) {
      out.open("res7.txt", ios::out|ios::app);
      if (out.is_open())
    out << "7\n";
      else
    cout << "unable to open res7 file";
      out.close();
      char *input = strtok(line, " ");
      int i=0;
      while (input!=NULL) {
    out.open("res7.txt", ios::out|ios::app);
    if (out.is_open())
      out << "8\n";
    else
      cout << "unable to open res7 file";
    out.close();
    parameters[i] = atof(input);
    input = strtok(NULL, " ");
    i++;
      }
      int rnum = rand();
      location = rnum%10000;
      out.open("res7.txt", ios::out|ios::app);
      if (out.is_open())
    out << "9\n";
      else
    cout << "unable to open res7 file";
      out.close();
      Pop p(parameters[0], parameters[1], 0.2, 0.1, parameters[2], location);
      out.open("res7.txt", ios::out|ios::app);
      if (out.is_open())
    out << "10\n";
      else
    cout << "unable to open res7 file";
      out.close();
      double L = exp(-parameters[3]);
      double FF_mean = p.FF_steady(parameters[3], L, 0.0);
      out.open("res7.txt", ios::out|ios::app);
      if (out.is_open())
    out << "11\n";
      else
    cout << "unable to open res7 file";
      out.close();
      out.open("res7.txt", ios::out|ios::app);
      if (out.is_open()) {
    out << "number of chromosomes is " << p.all_population[0]->ga->chromosome << "  alpha is " << parameters[2] << "  cost is " << parameters[1];
    out << "  a is " << parameters[0] << "  u is " << parameters[3] << "  location is " << location << "  FF steady is " << FF_mean << '\n';
      }
      else
    cout << "unable to open res7 file FF";
      out.close();
      out.open("res7.txt", ios::out|ios::app);
      if (out.is_open())
    out << "12\n";
      else
    cout << "unable to open res7 file";
      out.close();
      if (FF_mean>0.1) {
    out.open("res7.txt", ios::out|ios::app);
    if (out.is_open())
      out << "13\n";
    else
      cout << "unable to open res7 file";
    out.close();
    p.insert_O(0.05);
    out.open("res7.txt", ios::out|ios::app);
    if (out.is_open())
      out << "14\n";
    else
      cout << "unable to open res7 file";
    out.close();
    double total_mean = p.steady(parameters[3], L, 0.0);
    double perc = p.perc_O();
    out.open("res7.txt", ios::out|ios::app);
    if (out.is_open())
      out << "total steady is " << total_mean << "   perc O is " << perc << '\n';
    else
      cout << "unable to open res7 file";
    out.close();
      }
    }
  }
  else
    cout << "Unable to open input7 file\n";
    in.close();
  return 0;
}

I don't see any output on the console. When I run the code on a different machine, I see that I have a floating point exception. I guess I could find where it is, but what I don't understand is why I don't see any output or the error on the console in cygwin. Can anyone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼前雾蒙蒙 2025-01-04 06:38:19

你确定代码可以编译吗?在倒数第三行

in.close();*/

,但是没有开始 /* 标记。我会确保您的代码确实正在编译并且生成了一个新的 .exe。您可能无法编译和运行没有任何 cout 的旧 .exe。

另外,你确定没有抛出异常吗?我无法完全测试代码,因为我缺少“Pop.h”的源代码,因此我无法完全看到编译时遇到的错误。您能否尝试在程序顶部进行初始cout,并可能向 Pop.h 提供源,以便我们可以对其进行更多测试。

Are you sure the code compiles? At the third to last line you have

in.close();*/

However, there is no opening /* tag. I would make sure your code is actually compiling and you have a new .exe generated. You may be failing to compile and running an old .exe that does not have any cout's.

Also, are you sure there are no exceptions being thrown? I can not fully test the code because I am missing the source for "Pop.h", so I can not fully see what errors I get when I compile. Could you try to have an initial cout at the top of your program, and perhaps provide the soruce to Pop.h, so we may test it more.

掩饰不了的爱 2025-01-04 06:38:19

首先,异常甚至可能在不提供信息的情况下终止程序。 Cygwin 是一种将 Windows 和 UNIX 结合起来的尝试,但事情并不总是完美。

其次,std::cout 是一个缓冲流。因此,每个运算符<<都会添加到缓冲区,直到缓冲区填满并自动输出,您可以使用cout.flush()显式刷新缓冲区,或者隐式刷新缓冲区,例如 std::endl 会导致。

因此,您需要使用调试器或其他系统来查找错误并修复它。您还需要在多个平台上进行测试,因为事情在不同的平台上表现得不同——包括不同的错误,甚至看起来工作正常。

First, an exception may terminate a program without even giving information. Cygwin is an attempt to combine windows and unix and things don't always work perfectly.

Second, std::cout is a buffered stream. As such, each operator<< adds to the buffer until either the buffer fills and it is automatically output, you explicitly flush the buffer with cout.flush(), or implicitly flush the buffer such as std::endl will cause.

So, you need to use the debugger or the other system to find the bug and fix it. You also need to test on multiple platforms as things do behave differently on differently -- including different errors and even appearing to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文