1、已有文本文件:
string dataList;
使用fopen读取:
FILE *fpListFile = fopen(dataList.c_str(), "r");
if (!fpListFile){
cout << "0.can't open " << dataList << endl;
return -1;
}
方法一:
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接收的数据包括了换行符,所以在使用之前需先将删除换行符;