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

时间:2012-01-19 15:29:27

标签: c++ cygwin iostream

我在我的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中没有看到任何输出或控制台上的错误。任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

您确定代码是否已编译?在第三行到最后一行你有

in.close();*/

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

另外,你确定没有抛出异常吗?我无法完全测试代码,因为我错过了“Pop.h”的源代码,因此我无法完全看到编译时出现的错误。你可以尝试在程序的顶部有一个初始cout,也许可以为Pop.h提供帮助,所以我们可以测试它。

答案 1 :(得分:0)

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

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

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