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

No error or cout messages on cygwin console

提问人:user757141 提问时间:1/19/2012 最后编辑:Jonathan Senguser757141 更新时间:9/19/2012 访问量:873

问:

我在 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 的控制台上看不到任何输出或错误。谁能帮我?

C++ Cygwin IOstream

评论

0赞 ak2 1/19/2012
另外,您使用的是 Cygwin gcc 还是其他东西?
0赞 hmjd 1/20/2012
cout仅当无法打开“RES7”文件时才调用?有效吗?尝试添加 after(基本上总是在某个地方调用它)。printf()cout << "hello\n";printf()
0赞 Michael Kristofik 1/20/2012
你能试着把代码简化为一个简单的吗?该测试的结果应该对我们有所帮助。cout << "Hello, world!"

答:

0赞 josephthomas 1/20/2012 #1

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

in.close();*/

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

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

0赞 Jonathan Seng 9/19/2012 #2

首先,例外可能会在不提供信息的情况下终止程序。Cygwin 是将 windows 和 unix 结合在一起的尝试,但事情并不总是完美无缺。

其次,是缓冲流。因此,每个缓冲区都会添加到缓冲区中,直到缓冲区填满并自动输出,您显式刷新缓冲区,或者隐式刷新缓冲区,如将导致。std::coutoperator<<cout.flush()std::endl

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