这似乎应该是一个基本的C ++过程,但我不熟悉数据输出。
当我打印数据而不输出到文本文件时,我得到正确的值。
example: 00150017000 181
打印到文本文件时,这就是我得到的:
11161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116
这是我的代码:
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();
其他一切正常,我知道CRC生成正确。只需要获得正确的输出。
我可以通过添加&#34;&gt;输出控制台屏幕到文本文件CRC.txt&#34;调试属性命令参数,但我只想知道如何将ofstream方法合并到此。
提前致谢。
答案 0 :(得分:5)
您不会将您认为保存的内容保存到文件中。首先保存auto
函数的结果,在你的情况下为11.然后你保存sprintf()
函数的结果,在你的情况下是11 + 1(空格)+ 3 + 1(\ n)= 16.所以结果是1116的200倍。
你想做什么
printf()
答案 1 :(得分:3)
您正在退回sprintf()
和printf()
到该文件。 sprintf()
和printf()
的返回值为int
,而不是您创建的字符串。要输出字符串,您需要将代码更改为
for (i = 0; i < 200; i++, id++)
{
sprintf(idstring, "%011d", id);
myfile << idstring;
myfile << computeCrc(idstring) << endl;
}