首页 > 编程 > C++ > 正文

C++ fopen按行读取文件及所读取的数据问题

2019-11-06 07:58:21
字体:
来源:转载
供稿:网友

1、已有文本文件:

string dataList;使用fopen读取:

FILE *fpListFile = fopen(dataList.c_str(), "r");if (!fpListFile){	cout << "0.can't open " << dataList << endl;	return -1;}

2、按行读取数据:方法一:

char  loadImgPath[1000];while(EOF != fscanf(fpListFile, "%s", loadImgPath)){	...}其中,loadImgPath不能使用string类型,即使用loadImgPath.c_str()接收数据也不行,否则读取内容为空;

方法二:

char buff[1000];while(fgets(buff, 1000 ,fpListFile) != NULL){	char *pstr = strtok(buff, "/n");	...	}其中,buff接收的数据包括了换行符,所以在使用之前需先将删除换行符;


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选