文件写入与文件读取

进行文件写入与读取操作;
在文件读取部分有些不理解
#include<iostream> #include<fstream> using namespace std; struct tream { char name[12]; double rmb; }; int main() { int i; cin >> i; ofstream ofile; ofile.open("word.txt"); ofile << i << endl; tream *t = new tream[i]; int j; for (j = 0; j < i; j++) { cin>>t[j].name; cin >> t[j].rmb; } for (j = 0; j < i; j++) { ofile << t[j].name<<endl; ofile << t[j].rmb<<endl; } delete[]t; ofile.close(); ifstream ifile; ifile.open("word.txt"); if(!ifile.is_open()) cout << "文件打开错误"; int f; ifile >> f; cout << f << endl; tream *p = new tream[i]; for (j = 0; j < i; j++) { ifile >> p[j].name; cout << p[j].name << endl; ifile >> p[j].rmb; cout << p[j].rmb << endl; }//当在这个地方加上ifile.good()时文件读取了两次 与想法不符所以就没有用 if (ifile.eof()) cout << "end"; ifile.close(); return 0; }

总的来讲代码可以完成文本的写入与读取

但是没有读取和写入的检验错误的代码

只有一个简单的eof函数检验文件是否为空

而且在各个过程中不断乱定义指针

以及临时变量

第二次好像也没释放动态储存空间

这次程序就是这样

原文地址:https://www.cnblogs.com/qxhn/p/6085492.html