c++读取txt文件到string

来源:互联网 发布:阿里云推荐算法 编辑:程序博客网 时间:2024/06/18 14:10
string str_native_json("");


FILE* file;
long lSize;
char* szBuf;


file = fopen("native_video.txt", "r+");
if(file)
{
fseek(file, 0, SEEK_END);
lSize = ftell(file);
fseek(file, 0, SEEK_SET);
szBuf = new char[lSize + 1];
fread(szBuf, 1, lSize, file);
fclose(file);
szBuf[lSize] = 0;
str_native_json = szBuf;
delete szBuf;

}


定义函数:int fseek(FILE * stream, long offset, int whence);

函数说明:
fseek()用来移动文件流的读写位置. 

SEEK_SET: 文件开头
SEEK_CUR: 当前位置
SEEK_END: 文件结尾

欲将读写位置移动到文件开头时:fseek(FILE *stream, 0, SEEK_SET);
2) 欲将读写位置移动到文件尾时:fseek(FILE *stream, 0, 0SEEK_END);

0 0
原创粉丝点击