1、已有文本文件:
string dataList;
使用fopen讀?。?/p>
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接收的數據包括了換行符,所以在使用之前需先將刪除換行符。
以上這篇解決C++ fopen按行讀取文件及所讀取的數據問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答