这几天在写关于多线程和文件的操作,用到了打印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文件的内容啦!