BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #9286同步于 2008/7/6
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

[求助]帮忙看下这是为什么

loop
2008/7/6镜像同步7 回复
#include <iostream> #include <fstream> #include <iomanip> #include <cstdlib> inline void eatline(){while (std::cin.get() != '\n') continue;} const int LIM = 20; struct planet { char name[LIM]; double population; double g; }; const char * file = "planet.dat"; int main() { using namespace std; planet pl; cout << fixed << right; /* ifstream fin; fin.open(file, ios_base::in | ios_base::binary); if (fin.is_open()) { cout << "Here are the current contents of the " << file << " file: \n"; while (fin.read((char*)&pl, sizeof pl)) { cout << setw(LIM) << pl.name << ":" << setprecision(0) << setw(12) << pl.population << setprecision(2) << setw(6) << pl.g << endl; } fin.close(); } ofstream fout(file, ios_base::out | ios_base::binary | ios_base::app); if (!fout.is_open()) { cerr << "Can't open " << file << " file for output:\n"; exit(EXIT_FAILURE); } cout << "Enter planet name(enter a blank line to quit):\n"; cin.get(pl.name, LIM); while (pl.name[0]) { eatline(); cout << "Enter planetary population: "; cin >> pl.population; cout << "Enter planet's acceleration of gravity: "; cin >> pl.g; eatline(); fout.write( (char*)&pl, sizeof pl ); cout << "Enter planet name (enter a blank line to quit):\n"; cin.get(pl.name, LIM); } fout.close(); fin.clear(); fin.open(file, ios_base::in | ios_base::binary); if (fin.is_open()) { cout << "Here are the new contents of the " << file << " file: \n"; while (fin.read( (char*)&pl, sizeof pl ) ) { cout << setw(LIM) << pl.name << ":" << setprecision(0) << setw(12) << pl.population << setprecision(2) << setw(6) << pl.g << endl; } fin.close(); } else { cerr << "Error on opening\n"; exit(EXIT_FAILURE); } */ /**********************************************************************************/ fstream finout; finout.open(file, ios_base::in | ios_base::out | ios_base::binary); int ct = 0; if (finout.is_open()) { finout.seekg(0); // go to beginning cout << "Here are the current contents of the" << file << " file: \n"; while (finout.read( (char*)&pl, sizeof pl) ) { cout << ct++ << ": " << setw(LIM) << pl.name << ": " << setprecision(0) << setw(12) << pl.population << setprecision(2) << setw(6) << pl.g << endl; } if (finout.eof()) finout.clear(); else { cerr << "Error in reading " << file << ".\n"; exit(EXIT_FAILURE); } } else { cerr << file << " could not be opened -- bye.\n"; exit(EXIT_FAILURE); } cout << "Enter the record number you wish to change:"; int num; cin >> num; //加上前面被注释掉的代码这里为什么就输入不了了? eatline(); if (num < 0 || num >= ct) { cerr << "Invalid record number -- bye.\n"; exit(EXIT_FAILURE); } streampos place = num * sizeof pl; finout.seekg(place); if (finout.fail()) { cerr << "Error on attempted seek.\n"; exit(EXIT_FAILURE); } finout.read( (char*)&pl, sizeof pl ); cout << "Your selection:\n"; cout << num << ": " << setw(LIM) << pl.name << ": " << setprecision(0) << setw(12) << pl.population << setprecision(2) << setw(6) << pl.g << endl; if (finout.eof()) finout.clear(); cout << "Enter planet name:"; cin.get(pl.name, LIM); eatline(); cout << "Enter planetary population:"; cin >> pl.population; cout << "enter planet's acceleration of gravity:"; cin >> pl.g; finout.seekp(place); finout.write( (char*)&pl, sizeof pl ) << flush; if (finout.fail()) { cerr << "Error on attempted write\n"; exit(EXIT_FAILURE); } ct = 0; finout.seekg(0); cout << "Here are the current contents of the" << file << " file: \n"; while (finout.read( (char*)&pl, sizeof pl) ) { cout << ct++ << ": " << setw(LIM) << pl.name << ": " << setprecision(0) << setw(12) << pl.population << setprecision(2) << setw(6) << pl.g << endl; } if (finout.eof()) finout.clear(); else { cerr << "Error in reading " << file << ".\n"; exit(EXIT_FAILURE); } finout.close(); cout << "Bye.\n"; return 0; }
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
loop机器人#1 · 2008/7/6
整个的结构很简单 前面被 /*....*/ 注释掉的部分生成 .dat 文件 后面是用 fstream对象操作文件(读取,和修改)而已。 但是加上前面的部分后 cin >> num 处怎么输入不了了呢
loop机器人#2 · 2008/7/6
在 cin >> num; 前加 cin.clear(); 就可以,不过是为什么? 没人来么。。。。 【 在 loop (...) 的大作中提到: 】 : 整个的结构很简单 : 前面被 /*....*/ 注释掉的部分生成 .dat 文件 : 后面是用 fstream对象操作文件(读取,和修改)而已。 : ...................
purevirtual机器人#3 · 2008/7/6
程序太长了。。 如果在cin>>num前面加个cin.get()试试呢? 【 在 loop (很忧郁) 的大作中提到: 】 : 在 cin >> num; : 前加 cin.clear(); : 就可以,不过是为什么? : ...................
loop机器人#4 · 2008/7/6
试过,没效 结构很简单,虽然裹脚布。。。 【 在 purevirtual (天之健|杨无敌) 的大作中提到: 】 : 程序太长了。。 : 如果在cin>>num前面加个cin.get()试试呢?
loop机器人#5 · 2008/7/6
去web看轻松。。。 【 在 purevirtual (天之健|杨无敌) 的大作中提到: 】 : 程序太长了。。 : 如果在cin>>num前面加个cin.get()试试呢?
purevirtual机器人#6 · 2008/7/6
用惯了t,web刷新的缓慢让我有些不适应了。。、 【 在 loop (很忧郁) 的大作中提到: 】 : 去web看轻松。。。
ericyosho机器人#7 · 2008/7/7
只有一个小疑问,难道C++的cin还是没有解决行尾的问题么? 感觉你的那个eatline,对C++应该无效才对。 猜测,纯的。