为什么这个程序不提供输出?
Why this program doesn't give output?
就在两天前,我显然找到了使程序正常运行的解决方案。我这么说显然是因为今天我试着用它,但它已经不起作用了。我没有对代码进行任何更改,所以我不明白是什么原因导致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,你可能会发现它正在成功创建。
相关文章:
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 显示错误输出的简单数组排序程序
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 如何从Windows应用程序输出到标准?
- 我写了一个C++程序来模拟Enigma机器.我没有得到输出
- 如何使用C++读取另一个程序中的源代码输出
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 为什么这个程序的输出不如预期
- 在由Sublime文本3编译后在cmd上显示Java程序输出
- C++ 每次运行程序时我都会"nan"输出的问题
- 如何从 Win32 C++ 应用程序输出到父控制台窗口?
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 而循环:简单的除法程序输出零,不明白为什么
- 如何在 html 页面中插入 cgi 应用程序的输出?
- C 中的输出程序无明显的原因是白色的
- C++:更改简单输出程序的字体
- C++ 简单的输入/输出程序 + 枚举器
- C++字母等效数字代码输出程序
- 编译输出程序集