3C科技 娛樂遊戲 美食旅遊 時尚美妝 親子育兒 生活休閒 金融理財 健康運動 寰宇綜合

Zi 字媒體

2017-07-25T20:27:27+00:00
加入好友
[C/C++基礎] 使用C語言一行一行連續讀檔將檔案字串存放到C++的vector變數中,之後再依序從vector變數中將值取出並透過C的字元陣列顯示出來   vector、string、FILE、push_back、fopen、memset、size()、strcpy、c_str()、fgets Code: #include #include #include #include #include #include using namespace std; int main() { // 使用C語言方式把檔案內容讀進來 int i=0; FILE *pf='\0'; char buf[513]; string strbuf; memset(buf,0,sizeof(buf));// 清空記憶體 vector vec_file; vec_file.clear(); pf=fopen("filelist.txt","r"); while(fgets(buf,512,pf) != NULL) { buf[strlen(buf)-1]='\0'; vec_file.push_back(buf); } fclose(pf); for ( i = 0 ; i < vec_file.size() ; i++ ) { strbuf=vec_file[i]; strcpy(buf, strbuf.c_str()); printf("%s\n",buf); } return 0; }

本文由jashliaoeuwordpress提供 原文連結

寫了 5860316篇文章,獲得 23313次喜歡
精彩推薦