为什么这个程序不提供输出?

Why this program doesn't give output?

本文关键字:输出 程序 为什么      更新时间:2023-10-16

就在两天前,我显然找到了使程序正常运行的解决方案。我这么说显然是因为今天我试着用它,但它已经不起作用了。我没有对代码进行任何更改,所以我不明白是什么原因导致in不再执行。通常,这个程序应该打开一个.dat文件,读取并解析它,只提取我需要的列,在本例中是第二列。一旦排除列,它将使用数组按120个元素的集合计算数据的平均值。两天前它工作正常,今天它没有给出任何输出,调试函数也没有显示任何错误。代码是这样的:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
#define FATAL(MSG) do { 
    std::cerr << "Errore: " << MSG << 'n'; 
    exit(1); 
} while (false)
int main()
{
   std::ifstream fin ("P01-05 tensione di vapore di riferimento fino                        180°C.dat");
   if (fin.is_open())
   {
       std::ofstream fout("tmean.dat");
       if (fout.is_open())
       {
          fout << "Tmeann";
          std::string line;
          while (getline(fin, line))
         {
            const int group_size = 120;
            double temp[group_size];
            double total = 0;
            for (int i=0; i < group_size; ++i)
            {
                if (getline(fin, line))
                {
                    std::istringstream ss(line);
                    double time;
                    if (ss >> time >> temp[i])
                        total += temp[i];
                    else
                        FATAL("unable to parse 2 doubles from line '"
                              << line << "' for [" << i << ']');
                }
                else
                    // will rarely happen after checking !eof()
                    FATAL("failed to read needed line from file for ["
                          << i << ']');
            }
            double tmean = total / group_size;
            fout << tmean << 'n';       
        }
    }
    else
        FATAL("could not open output file.");
        fout.close(); 
}
else
    FATAL("non si puo aprire il file.");
    fin.close();
return 0;
}

你的程序为我编译并运行。你的代码中没有致命的stdio消息可能意味着一切都在运行。我能够使所有FATAL消息执行。确认dioporco.dat没有被写入文件系统的其他地方。可能发生的情况是,当你运行程序时,"当前目录"并不是你想象的那样,程序正在其他地方运行,因此dioporco.dat被写在"那里"。您也可以尝试将dioporco.dat更改为一个完全限定的名称,如"/home/myuser/dioporco.dat"。由于现在编写了打开文件,该文件将在操作系统认为程序执行时设置了当前目录的任何位置创建。在你的文件系统中搜索dioporco.dat,你可能会发现它正在成功创建。