程序出错,错误已经找到,但是不太理解,求详解
程序代码:
#include <iostream> #include <fstream> #define SIZE 60 char filename[SIZE]; using namespace std; struct Dntnews { char name[SIZE]; double money; }; void rname(ifstream,Dntnews *); int main(void) { ifstream iDfile; Dntnews *Dp = new Dntnews; /*open the file */ ofstream oDfile; again: cout << "请输入需要创建的文件路径及文件名: "; cin.getline(filename,SIZE); oDfile.open(filename); if(!oDfile.is_open()) { cout << "文件创建失败,请检查文件路径是否正确!\n\n"; goto again; } oDfile << "The donation people's list \n\nName\t\tMoney\n"; /* open to end */ cout << "请输入捐款人数目: "; int people = 0; cin >> people; for(int i = 1; i <= people; i++) { cout << "请输入捐款人姓名: "; (cin >> Dp->name).get(); cout << "请输入捐款钱数: "; cin >> Dp->money; //write to the Dfile oDfile << '@'; //name's mark oDfile << Dp->name; oDfile << "\t\t"; oDfile << '$'; //money's mark oDfile << Dp->money; oDfile << "\n"; } oDfile.close(); iDfile.open(filename); for(i = 1; i <= people; i++) { cout << i << endl; //rname(iDfile,Dp); char ch = 0; for(;;) { if(!(iDfile >> ch)) { cout << "到达文件尾" << ch; break; } cout << ch << " == ch\n"; if(ch == '@') { iDfile >> Dp->name; continue; } if(ch == '$') { iDfile >> Dp->money; cout << "运行\n"; break; } } //end cout << "运行2" << people << i << endl; if(Dp->money >= 10000) { cout << Dp->name << "捐赠了" << Dp->money; if(Dp->money > 36500) cout << "元! 感谢" << Dp->name << endl; else cout << "元." << endl; } cout << "运行3"; } cout << "运行4" << endl; delete Dp; cout << "运行5" << endl; iDfile.close(); cout << "运行6" << endl; return 0; } void rname(ifstream iDfile,Dntnews *Dp) { char ch = 0; for(;;) { if(!(iDfile >> ch)) { cout << "到达文件尾" << ch; break; } cout << ch << " == ch\n"; if(ch == '@') { iDfile >> Dp->name; continue; } if(ch == '$') { iDfile >> Dp->money; cout << "运行\n"; break; } } }这个程序可以正常运行,但是不用
程序代码:
//rname(iDfile,Dp); char ch = 0; for(;;) { if(!(iDfile >> ch)) { cout << "到达文件尾" << ch; break; } cout << ch << " == ch\n"; if(ch == '@') { iDfile >> Dp->name; continue; } if(ch == '$') { iDfile >> Dp->money; cout << "运行\n"; break; } } //end
这一段而调用rname函数程序(其内容一模一样)就会读取文件读取出错
在循环中第二次调用函数的时候文件中的读取位置已经不对了
我想应该是带入函数时变元创建副本的问题,但是第二次为什么直接就出错了呢?就算不在上次读取的位置上也应该在开始的位置啊?不懂求解,分全给,望赐教...
我整理了一下,大致只需看
程序代码:
#include <iostream> #include <fstream> #define SIZE 60 char filename[SIZE]; using namespace std; struct Dntnews { char name[SIZE]; double money; }; void rname(ifstream,Dntnews *); int main(void) { ifstream iDfile; Dntnews *Dp = new Dntnews; /*open the file */ ofstream oDfile; again: cout << "请输入需要创建的文件路径及文件名: "; cin.getline(filename,SIZE); cout << "请输入捐款人数目: "; int people = 0; cin >> people; iDfile.open(filename); for(i = 1; i <= people; i++) { cout << i << endl; //rname(iDfile,Dp); char ch = 0; for(;;) { if(!(iDfile >> ch)) { cout << "到达文件尾" << ch; break; } cout << ch << " == ch\n"; if(ch == '@') //@和$分别是文件中名字和钱的位置,这里无错,不用考虑 { iDfile >> Dp->name; continue; } if(ch == '$') { iDfile >> Dp->money; break; } } //end } return 0; } void rname(ifstream iDfile,Dntnews *Dp) { char ch = 0; for(;;) { if(!(iDfile >> ch)) { cout << "到达文件尾" << ch; break; } cout << ch << " == ch\n"; if(ch == '@') { iDfile >> Dp->name; continue; } if(ch == '$') { iDfile >> Dp->money; cout << "运行\n"; break; } } }