怎么读取txt文件中的内容?
包括读取其中的内容和写入,
请贴完整代码及原理注释
[解决办法]
#include <iostream>#include <fstream>using namespace std;int main(int argc, char** argv){ char aline[1024]; // 想文本文件写入 ofstream fos("e:/file.txt"); if(!fos) { cout << "Can not open file..." << endl; } fos << "This is line 1 to be written into file.txt" << endl; fos << "This is line 2 to be written into file.txt" << endl; fos << "This is line 3 to be written into file.txt" << endl; fos.close(); // 读文本文件内容 ifstream fis("e:/file.txt"); if(!fis) { cout << "Can not open file..." << endl; } while(fis) { memset(aline, 0, 100); fis.getline(aline, 100); if(aline[0] == 0) break; cout << aline << endl; } return 0;}