使用 (FILE*) 读取大文本文件后出现垃圾
Garbage after reading a large text file using (FILE*)
我有一个用 C/C++ 编写的示例程序,它将文本文件读入内存。在尝试解析此文件(不是此示例的一部分)时,我在文件末尾附近遇到了很多垃圾。对此进行调查,我发现将大文件读入内存时出现了一些问题;小型文本文件不会发生此问题。这是我的代码:
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
char* readFile_(char* fname)
{
char* rv=NULL;
int bytes=0;
FILE* pfile = NULL;
pfile = fopen( fname, "r" );
if ( pfile )
{
fseek(pfile, 0, SEEK_END);
bytes = ftell(pfile);
fseek(pfile, 0, SEEK_SET);
rv = new char[bytes+1];
memset(rv,0,bytes+1);
fread( rv, bytes, 1, pfile );
fclose(pfile);
}
return rv;
}
int main(int argc, char **argv)
{
char* filebuffer = NULL;
filebuffer = readFile_( "mv2.txt" );
FILE* pfile = fopen("op.txt", "w");
int len = strlen(filebuffer);
fwrite( filebuffer, len, 1, pfile );
fclose(pfile);
delete[] filebuffer;
return 0;
}
供参考,文件托管于此:
mv2.txt 文件:https://gist.github.com/anonymous/bb101393729d3ada944f
op.txt 文件:https://gist.github.com/anonymous/93595c83ad62e40d0f0a
任何人都可以突出显示问题所在吗?
编辑:我正在使用 Windows (Windows 7 OS)
编辑 2:感谢大家帮助我找到问题,这里是根据您的一些反馈更新的代码,即使对于一些非常大的文本文件,它似乎也能解决我的问题:
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
char* readFile_(char* fname)
{
char* rv=NULL;
long bytes=0;
FILE* pfile = NULL;
pfile = fopen( fname, "rb" );
if ( pfile )
{
fseek(pfile, 0, SEEK_END);
bytes = ftell(pfile);
fseek(pfile, 0, SEEK_SET);
rv = new char[bytes+1];
memset(rv,0,bytes+1);
fread( rv, bytes, 1, pfile );
fclose(pfile);
}
return rv;
}
int main(int argc, char **argv)
{
char* filebuffer = NULL;
filebuffer = readFile_( "mv2.txt" );
FILE* pfile = fopen("op.txt", "wb");
int len = strlen(filebuffer);
fwrite( filebuffer, len, 1, pfile );
fclose(pfile);
delete[] filebuffer;
return 0;
}
通过包含 conio.h,我可以假设您在 Windows。在Windows中,不使用二进制模式读取,你会遇到这样的情况。我会尝试以 "rb" 作为模式打开文件。
FILE* pfile = NULL;
pfile = fopen( fname, "rb" );
我有一个用 C/C++ 编写的示例程序,它将文本文件读入内存。在尝试解析此文件(不是此示例的一部分)时,我在文件末尾附近遇到了很多垃圾。对此进行调查,我发现将大文件读入内存时出现了一些问题;小型文本文件不会发生此问题。这是我的代码:
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
char* readFile_(char* fname)
{
char* rv=NULL;
int bytes=0;
FILE* pfile = NULL;
pfile = fopen( fname, "r" );
if ( pfile )
{
fseek(pfile, 0, SEEK_END);
bytes = ftell(pfile);
fseek(pfile, 0, SEEK_SET);
rv = new char[bytes+1];
memset(rv,0,bytes+1);
fread( rv, bytes, 1, pfile );
fclose(pfile);
}
return rv;
}
int main(int argc, char **argv)
{
char* filebuffer = NULL;
filebuffer = readFile_( "mv2.txt" );
FILE* pfile = fopen("op.txt", "w");
int len = strlen(filebuffer);
fwrite( filebuffer, len, 1, pfile );
fclose(pfile);
delete[] filebuffer;
return 0;
}
供参考,文件托管于此:
mv2.txt 文件:https://gist.github.com/anonymous/bb101393729d3ada944f
op.txt 文件:https://gist.github.com/anonymous/93595c83ad62e40d0f0a
任何人都可以突出显示问题所在吗?
编辑:我正在使用 Windows (Windows 7 OS)
编辑 2:感谢大家帮助我找到问题,这里是根据您的一些反馈更新的代码,即使对于一些非常大的文本文件,它似乎也能解决我的问题:
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
char* readFile_(char* fname)
{
char* rv=NULL;
long bytes=0;
FILE* pfile = NULL;
pfile = fopen( fname, "rb" );
if ( pfile )
{
fseek(pfile, 0, SEEK_END);
bytes = ftell(pfile);
fseek(pfile, 0, SEEK_SET);
rv = new char[bytes+1];
memset(rv,0,bytes+1);
fread( rv, bytes, 1, pfile );
fclose(pfile);
}
return rv;
}
int main(int argc, char **argv)
{
char* filebuffer = NULL;
filebuffer = readFile_( "mv2.txt" );
FILE* pfile = fopen("op.txt", "wb");
int len = strlen(filebuffer);
fwrite( filebuffer, len, 1, pfile );
fclose(pfile);
delete[] filebuffer;
return 0;
}
通过包含 conio.h,我可以假设您在 Windows。在Windows中,不使用二进制模式读取,你会遇到这样的情况。我会尝试以 "rb" 作为模式打开文件。
FILE* pfile = NULL;
pfile = fopen( fname, "rb" );