简体   繁体   English

将CRC数据写入.txt文件

[英]Writing CRC data to .txt file

This seems like it should be a basic C++ process, but I'm not familiar with the data output. 看来这应该是一个基本的C ++程序,但是我对数据输出并不熟悉。

When I print the data without outputting to a text file, I get the correct values. 当我打印数据而不输出到文本文件时,我得到了正确的值。

example: 00150017000 181

When printing to a text file, this is what I get: 当打印到文本文件时,这是我得到的:

11161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116111611161116

Here is my code: 这是我的代码:

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();

Everything else works fine and I know the CRC is generated correctly. 其他一切工作正常,我知道CRC生成正确。 Just a matter of getting the correct output. 只需获得正确的输出即可。

I was able to output the console screen to a text file by adding "> CRC.txt" to the Debugging Properties Command Arguments, but I just wanted to know how I could incorporate the ofstream method into this. 通过在调试属性命令自变量中添加“> CRC.txt”,我能够将控制台屏幕输出到文本文件,但是我只是想知道如何将ofstream方法合并到其中。

Thanks in advance. 提前致谢。

You don't save to the file what you think you save. 您不会将您认为保存的内容保存到文件中。 First you save the result of sprintf() function, which in your case is 11. Then you save the result of the printf() function which in your case is 11 + 1 (space) + 3 + 1 (\\n) = 16. So the result is 200 times 1116. 首先,保存sprintf()函数的结果,在您的情况下为11。然后保存printf()函数的结果,在您的情况下为11 +1(空格)+ 3 +1(\\ n)= 16因此,结果是1116的200倍。

What you wanted to do 你想做什么

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();

You are ouputing the return of sprintf() and printf() to the file. 您正在将sprintf()printf()返回到文件。 The returns of sprintf() and printf() are int and not the string you are creating. sprintf()printf()的返回值是int而不是您正在创建的字符串。 To output the string you need to change your code to 要输出字符串,您需要将代码更改为

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.

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