這幾天在寫關於多線程和文件的操作,用到了打印txt文件這個操作,就把這個文件操作的要點簡單的記錄一下。
代碼如下:
1 cout << "主線程打印txt文本內容:\n" << endl; 2 cout << "==========================" << endl; 3 ifstream inFile("E:\\UpupooGame\\TEST.txt"); 4 string str; 5 while (inFile.good()) 6 { 7 getline(inFile, str); //該方法來自<string> 8 cout << str << endl; 9 } 10 cout << "==========================" << endl; 11 /*cin.get();*/
文件路徑是自己設置的,這樣如果文件不為空的話,就能在控制台按行打印出txt文件的內容啦!