1 // random.cpp -- random access to a binary file 2 #include <iostream> // not required by most systems 3 #include <fstream> 4 #include <iomanip> 5 #include <cstdlib> // (or stdlib.h) for exit() 6 const int LIM = 20; 7 struct planet 8 { 9 char name[LIM]; // name of planet 10 double population; // its population 11 double g; // its acceleration of gravity 12 }; 13 14 const char * file = "planet.dat"; // ASSUMED TO EXIST (binary.cpp example) 15 inline void eatline() { while (std::cin.get() != ‘\n‘) continue; } //内联函数,编译的时候在调用的地方替换代码 16 //用来解决函数调用的效率问题 17 18 int main() 19 { 20 using namespace std; //在主函数使用标准程序库的命名空间 21 planet pl; 22 cout << fixed; //数字定点形式输出 23 24 // show initial contents 25 fstream finout; //建立文件流,名字叫做 finout 26 finout.open(file, 27 ios_base::in | ios_base::out | ios_base::binary); /*调用finout子函数open,ios_base::in ,ios_base::out使用输入输出,i 28 os_base::binary表示用二进制操作文件数据 */ 29 //NOTE: Some Unix systems require omitting | ios::binary 30 int ct = 0; 31 if (finout.is_open()) //finout.is_open()返回整数表示打开文件是否成功 32 { 33 finout.seekg(0); // 将文件指针定位于文件的其实位置0(第一个字符), FileNam.seekg(Num); 34 cout << "Here are the current contents of the " 35 << file << " file:\n"; 36 37 /*finout.read读取文件,并都转化字符输出(原来为二进制) 38 FileName.read( (Type*) &struct,sizeof) */ 39 while (finout.read( (char *) &pl, sizeof pl)) 40 { 41 cout << ct++ << ": " << setw(LIM) << pl.name << ": " 42 << setprecision(0) << setw(12) << pl.population 43 << setprecision(2) << setw(6) << pl.g << endl; 44 } 45 if (finout.eof()) //到达文件尾清除错误信息 46 finout.clear(); // clear eof flag 47 else 48 { 49 cerr << "Error in reading " << file << ".\n"; 50 exit(EXIT_FAILURE); 51 } 52 } 53 else 54 { 55 cerr << file << " could not be opened -- bye.\n"; 56 exit(EXIT_FAILURE); 57 } 58 59 // change a record 60 cout << "Enter the record number you wish to change: "; 61 long rec; 62 cin >> rec; 63 eatline(); // 吸收换行之前的输入 用于输入字符或者下次要输入字符的时候 64 if (rec < 0 || rec >= ct) 65 { 66 cerr << "Invalid record number -- bye\n"; 67 exit(EXIT_FAILURE); 68 } 69 streampos place = rec * sizeof pl; // 文件指针位置的数据类型 place = rec * sizeof pl 将place指向一个固定位置 70 finout.seekg(place); // random access 71 if (finout.fail()) 72 { 73 cerr << "Error on attempted seek\n"; 74 exit(EXIT_FAILURE); 75 } 76 77 finout.read((char *) &pl, sizeof pl); //输出之前要先打开文件 78 cout << "Your selection:\n"; 79 cout << rec << ": " << setw(LIM) << pl.name << ": " 80 << setprecision(0) << setw(12) << pl.population 81 << setprecision(2) << setw(6) << pl.g << endl; 82 if (finout.eof() ) 83 finout.clear(); // clear eof flag 84 85 cout << "Enter planet name: "; 86 cin.get(pl.name, LIM); //读入字符:cin.get(Name,sizeof(Type) ) 87 eatline(); 88 cout << "Enter planetary population: "; 89 cin >> pl.population; 90 eatline(); 91 cout << "Enter planet‘s acceleration of gravity: "; 92 cin >> pl.g; 93 finout.seekp(place); // 对文件进行操作后指针会变化 重新定位指针 94 finout.write((char *) &pl, sizeof pl) << flush; //防止程序中断来不及送出数据 <<flush 95 if (finout.fail()) 96 { 97 cerr << "Error on attempted write\n"; 98 exit(EXIT_FAILURE); 99 } 100 101 // show revised file 102 ct = 0; 103 finout.seekg(0); // 接下来输出全部内容,将指针定位与开始位置 104 cout << "Here are the new contents of the " << file 105 << " file:\n"; 106 while (finout.read((char *) &pl, sizeof pl)) 107 { 108 cout << ct++ << ": " << setw(LIM) << pl.name << ": " 109 << setprecision(0) << setw(12) << pl.population 110 << setprecision(2) << setw(6) << pl.g << endl; 111 } 112 finout.close(); //关闭文件 113 cout << "Done.\n"; 114 // keeping output window open 115 // cin.clear(); 116 // eatline(); 117 // cin.get(); 118 return 0; 119 }
时间: 2024-10-23 12:33:01