这似乎应该是一个基本的C++过程,但我不熟悉数据输出.
当我打印数据而不输出到文本文件时,我得到正确的值.
example: 00150017000 181
Run Code Online (Sandbox Code Playgroud)
打印到文本文件时,这是我得到的:
11161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
ofstream myfile;
myfile.open("C:\\CRC.txt");
for (i = 0; i < 200; i++, id++)
{
myfile << sprintf(idstring, "%011d", id);
myfile << printf("%s %03d\n", idstring, computeCrc(idstring));
}
myfile.close();
Run Code Online (Sandbox Code Playgroud)
其他一切正常,我知道CRC生成正确.只需获得正确的输出.
通过将"> CRC.txt"添加到调试属性命令参数,我能够将控制台屏幕输出到文本文件,但我只是想知道如何将ofstream方法合并到此中.
提前致谢.
您不会将您认为保存的内容保存到文件中.首先保存sprintf()函数的结果,在你的情况下为11.然后你保存printf()函数的结果,在你的情况下是11 + 1(空格)+ 3 + 1(\n)= 16.所以结果是200时间1116.
你想做什么
char tempBuf[12];
ofstream myfile;
myfile.open("C:\\CRC.txt");
for (i = 0; i < 200; i++, id++)
{
sprintf(tempBuf, "%011d", id);
myfile << tempBuf << ' ';
sprintf(tempBuf, "%03d", computeCrc(tempBuf));
myFile << tempBuf << '\n';
}
myfile.close();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
296 次 |
| 最近记录: |