C++复习03---文件操作写入和读取

mac2024-10-28  14

// 写相应的东西到指定的目录中,并查询文件中的信息 void test03() { ofstream writefile; writefile.open("D:\\VS code\\book.txt"); // 这样的绝对路径可以识别 writefile << "C++程序设计" << " " << "xiaoxiao" << " " << "清华大学出版社" << " " << 36 << endl; writefile << "Java程序设计" << " " << "xiaoliu" << " " << "xx大学出版社" << " " << 56 << endl; writefile << "Python程序设计" << " "<< "xiaozhang" << " " << "jj大学出版社" << " " << 45 << endl; writefile << "C#程序设计" << " " << "xiaowang" << " " << "vv大学出版社" << " " << 75 << endl; writefile.close(); cout << "写入成功!" << endl; ifstream readfile; readfile.open("book.txt", ios::in); if (!readfile.is_open()) { cout << "Error opening file" << endl; } string s; cout << "文件读取如下:" << endl; while (!readfile.eof()) { //可以就按照这种方式读取文件 getline(readfile, s); cout << s << endl; } readfile.close(); }

 

最新回复(0)