读文件为什么没读完
把一个data.txt的文件,按行取出来,然后再写入另一个文件changedata.txt。
wchar_t filename[1024];
wchar_t str[]=L"f:\\data.txt";
wcscpy(filename,str);
wfstream infile;
wchar_t s[1024];
infile.open(filename,ios::in);
wofstream outfile;
outfile.open("f:\\changedata.txt");
while(infile.getline(s,1024))
{
outfile<<s<<endl;
}
data.txt有点大,大约3000行,程序执行了之后,changedata.txt中只有2022行,请问为甚呢?
[解决办法]
摒弃fstream
使用FILE *
不要把
fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待
和
fopen("...","...b");fread,fwrite,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待
弄混了
推荐使用WinHex软件查看文件或内存中的原始字节内容。
[解决办法]