繁体   English   中英

C ++文件输出

[英]C++ file output

所有,

我不是编程白痴,但是下面的代码产生0字节的文件。 我确认文件名正确,并且文件已创建。 我什至努力使每个人都可以读取,写入和执行文件,并指定文件被截断而不是每次都重新创建,并且仍然是0字节的文件。

fstream fs;
fs.clear();
fs.open(dataFileName.c_str(), fstream::out| fstream::trunc);
std::cout << dataFileName.c_str() << std::endl;

for (int idx = 0; idx < theNumberHorizontalPoints; ++idx)
{
   for (int zdx = 0; zdx < theVerticalProfilePtr->getNumberVerticalLevels(); ++zdx)
   {
      fs << theThermalArray[idx][zdx] << " ";
   }
   fs << std::endl;
   fs.flush();
}
fs.close();

代码的重要部分丢失了。 您的阵列尺寸是多少?

其余代码似乎很好,因此我编写了一个小型测试应用程序,将数字[1-6]写入名为demo.txt的文件中。

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    fstream fs;
    fs.clear();
    fs.open("demo.txt", fstream::out | fstream::trunc);

    int theThermalArray[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
    int theNumberHorizontalPoints = 2;

    for (int idx = 0; idx < theNumberHorizontalPoints; ++idx)
    {
       for (int zdx = 0; zdx < 3; ++zdx)
       {
          fs << theThermalArray[idx][zdx] << " ";
       }
       fs << std::endl;
       fs.flush();
    }
    fs.close();
}

如果NumberHorizo​​ntalPoints为零或更少,则将给出您描述的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM