[英]Writing CRC data to .txt file
看來這應該是一個基本的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生成正確。 只需獲得正確的輸出即可。
通過在調試屬性命令自變量中添加“> CRC.txt”,我能夠將控制台屏幕輸出到文本文件,但是我只是想知道如何將ofstream方法合並到其中。
提前致謝。
您不會將您認為保存的內容保存到文件中。 首先,保存sprintf()
函數的結果,在您的情況下為11。然后保存printf()
函數的結果,在您的情況下為11 +1(空格)+ 3 +1(\\ n)= 16因此,結果是1116的200倍。
你想做什么
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();
您正在將sprintf()
和printf()
返回到文件。 sprintf()
和printf()
的返回值是int
而不是您正在創建的字符串。 要輸出字符串,您需要將代碼更改為
for (i = 0; i < 200; i++, id++)
{
sprintf(idstring, "%011d", id);
myfile << idstring;
myfile << computeCrc(idstring) << endl;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.