C++語言基礎 例程 檔案的隨機讀寫
阿新 • • 發佈:2019-02-08
示例:寫到尾再從頭讀
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main( ) { int a[10], b[10]; fstream iofile("f1.dat",ios::in|ios::out); if(!iofile) { cerr<<"open error!"<<endl; exit(1); } cout<<"enter 10 integer numbers:"<<endl; for(int i=0; i<10; i++) { cin>>a[i]; iofile<<a[i]<<" "; } cout<<"The numbers have been writen to file. "<<endl; cout<<"Display the data by read from file: "<<endl; iofile.seekg(0, ios::beg); //檔案指標重回檔案開始位置 for(int i=0; i<10; i++) { iofile>>b[i]; cout<<b[i]<<" "; } cout<<endl; iofile.close(); return 0; }
學生資料處理
#include<iostream> #include <fstream> #include<cstdlib> #include<cstring> using namespace std; struct student { int num; char name[20]; float score; }; int main( ) { student stud[5]= {{1001,"Li",85},{1002,"Fun",97.5},{1004,"Wang",54}, {1006,"Tan",76.5},{1010,"ling",96} }; fstream iofile("stud.dat",ios::in|ios::out|ios::binary); //用fstream類定義輸入輸出二進位制檔案流物件iofile if(!iofile) { cerr<<"open error!"<<endl; abort( ); } //(1)向磁碟檔案輸出5個學生的資料並顯示出來 cout<<"(1)向磁碟檔案輸出5個學生的資料並顯示出來"<<endl; for(int i=0; i<5; i++) { iofile.write((char *)&stud[i],sizeof(stud[i])); cout<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score<<endl; } //(2)將磁碟檔案中的第1,3,5個學生資料讀入程式,並顯示出來; cout<<"(2)將磁碟檔案中的第1,3,5個學生資料讀入程式,並顯示出來"<<endl; student stud1[5]; //用來存放從磁碟檔案讀入的資料 for(int i=0; i<5; i=i+2) { iofile.seekg(i*sizeof(stud[i]),ios::beg); //定位於第0,2,4學生資料開頭 iofile.read((char *)&stud1[i/2],sizeof(stud1[0])); //先後讀入3個學生的資料,存放在stud1[0],stud[1]和stud[2]中 cout<<stud1[i/2].num<<" "<<stud1[i/2].name<<" "<<stud1[i/2].score<<endl; //輸出stud1[0],stud[1]和stud[2]各成員的值 } cout<<endl; //(3) 將第3個學生的資料修改後存回磁碟檔案中的原有位置。 cout<<"(3)將第3個學生的資料修改後存回磁碟檔案中的原有位置"<<endl; stud[2].num=1012; //修改第3個學生(序號為2)的資料 strcpy(stud[2].name,"Wu"); stud[2].score=60; iofile.seekp(2*sizeof(stud[0]),ios::beg); //定位於第3個學生資料的開頭 iofile.write((char *)&stud[2],sizeof(stud[2])); //更新第3個學生資料 iofile.seekg(0,ios::beg); //重新定位於檔案開頭 //(4)從磁碟檔案讀入修改後的5個學生的資料並顯示出來。 cout<<"(4)從磁碟檔案讀入修改後的5個學生的資料並顯示出來"<<endl; for(int i=0; i<5; i++) { iofile.read((char *)&stud[i],sizeof(stud[i])); //讀入5個學生的資料 cout<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score<<endl; } iofile.close( ); return 0; }
學生資料處理(OO版)
#include<iostream> #include <fstream> #include<cstdlib> #include<cstring> using namespace std; class Student { public: Student(void) {} Student(int n, char nam[20], float s):num(n),score(s) { strcpy(name,nam); } void setNum(int n) { num=n; } void setName(char nam[20]) { strcpy(name,nam); } void setScore(float s) { score=s; } void show() { cout<<num<<" "<<name<<" "<<score<<endl; //顯示通過<<的過載實現更自然 } private: int num; char name[20]; float score; }; int main( ) { Student stud[5]= { Student(1001,"Li",85), Student(1002,"Fun",97.5), Student(1004,"Wang",54), Student(1006,"Tan",76.5), Student(1010,"ling",96) }; fstream iofile("stud.dat",ios::in|ios::out|ios::binary); //用fstream類定義輸入輸出二進位制檔案流物件iofile if(!iofile) { cerr<<"open error!"<<endl; abort( ); } //(1)向磁碟檔案輸出5個學生的資料並顯示出來 cout<<"(1)向磁碟檔案輸出5個學生的資料並顯示出來"<<endl; for(int i=0; i<5; i++) { iofile.write((char *)&stud[i],sizeof(stud[i])); stud[i].show(); } //(2)將磁碟檔案中的第1,3,5個學生資料讀入程式,並顯示出來; cout<<"(2)將磁碟檔案中的第1,3,5個學生資料讀入程式,並顯示出來"<<endl; Student stud1[5]; //用來存放從磁碟檔案讀入的資料 for(int i=0; i<5; i=i+2) { iofile.seekg(i*sizeof(stud[i]),ios::beg); //定位於第0,2,4學生資料開頭 iofile.read((char *)&stud1[i/2],sizeof(stud1[0])); //先後讀入3個學生的資料,存放在stud1[0],stud[1]和stud[2]中 stud1[i/2].show(); //輸出stud1[0],stud[1]和stud[2]各成員的值 } cout<<endl; //(3) 將第3個學生的資料修改後存回磁碟檔案中的原有位置。 cout<<"(3)將第3個學生的資料修改後存回磁碟檔案中的原有位置"<<endl; stud[2].setNum(1012); //修改第3個學生(序號為2)的資料 stud[2].setName("Wu"); stud[2].setScore(60); iofile.seekp(2*sizeof(stud[0]),ios::beg); //定位於第3個學生資料的開頭 iofile.write((char *)&stud[2],sizeof(stud[2])); //更新第3個學生資料 iofile.seekg(0,ios::beg); //重新定位於檔案開頭 //(4)從磁碟檔案讀入修改後的5個學生的資料並顯示出來。 cout<<"(4)從磁碟檔案讀入修改後的5個學生的資料並顯示出來"<<endl; for(int i=0; i<5; i++) { iofile.read((char *)&stud[i],sizeof(stud[i])); //讀入5個學生的資料 stud[i].show(); } iofile.close( ); return 0; }