// file3.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。 // // #include "stdafx.h" #include <fstream> #include <iostream> using namespace std; struct Student{ char name[20]; int num; }; int addInFile() { ofstream outFile("botao.dat",ios::out|ios::binary); //定義文件輸出流 文件不存在時(shí)創(chuàng)建文件 //對(duì)文件打開錯(cuò)誤時(shí)的操作 if(!outFile) { cout<<"The file open error!"<<endl; return 0; } else //文件正常打開時(shí),進(jìn)行相應(yīng)的處理 { Student *s=new Student; cout<<"輸入學(xué)生姓名:"; cin>>s->name; cout<<"輸入學(xué)生學(xué)號(hào):"; cin>>s->num; outFile.write((char*)s,sizeof(Student)); //文件輸出流向文件中寫入student信息 } outFile.close(); //關(guān)閉輸出流 return 1; } int myReadFile() { ifstream inFile("botao.dat",ios::in|ios::binary); //文件輸入流 將文件中的student信息讀出到屏幕上 //對(duì)文件打開錯(cuò)誤時(shí)的操作 if(!inFile) { cout<<"The inFile open error!"<<endl; return 0; } else { Student *s=new Student; inFile.read((char*)s,sizeof(Student)); cout<<"姓名:"<<s->name<<endl; cout<<"學(xué)號(hào):"<<s->num<<endl; } inFile.close(); //關(guān)閉輸入流 } void addCustomers(char *identityCard,int number,int isExist) // 添加顧客信息 想總的顧客信息索引表中添加數(shù)據(jù) { ifstream inf("botao.dat",ios::in|ios::binary); if(!inf) { cout<<"文件不存在,正在創(chuàng)建……"<<endl; ofstream outf("botao.dat",ios::out|ios::binary); outf.close(); } inf.close(); fstream outFile("botao.dat",ios::app|ios::in|ios::binary); Student *index=new Student; if(!outFile) { cout<<"Open Error!"<<endl; //exit(1); } else { if(isExist==0) //如果此顧客還沒(méi)有在此航空公司買過(guò)票 { outFile.seekp(ios::end); //文件指針重新定位 index->num=0; strcpy(index->name,identityCard); index->num=number; outFile.write((char*)index,sizeof(Student)); //*****將信息寫入到文件中 cout<<"index->name:"<<index->name<<endl; cout<<"index ->num:"<<index->num<<endl; //outFile.flush(); //將緩沖中數(shù)據(jù)刷如到文件中 } else //如果此顧客已經(jīng)存在 { while(outFile.read((char*)index,sizeof(Student))) // { if(strcmp(identityCard,index->name)==0) { cout<<"指針移動(dòng)前:--------"<<outFile.tellg()<<endl; outFile.seekg(-(long)sizeof(Student),ios::cur); cout<<"指針移動(dòng)后:--------"<<outFile.tellg()<<endl; index->num=0; strcpy(index->name,identityCard); index->num=number; cout<<"index->name:"<<index->name<<endl; cout<<"index->num:"<<index->num<<endl; if(outFile.write((char*)index,sizeof(Student))) //*****將修改后信息寫入到文件中 cout<<"cheng gong ________________________"<<endl; //myReadFile(); ifstream iinFile("botao.dat",ios::in|ios::binary); //Student *s=new Student; iinFile.seekg(-(long)sizeof(Student),ios::cur); //將指針定位到修改信息后的結(jié)構(gòu)體前面 iinFile.read((char*)index,sizeof(Student)); cout<<"姓名:"<<index->name<<endl; cout<<"學(xué)號(hào):"<<index->num<<endl; iinFile.close(); break; } index=new Student; } } } //delete index; outFile.close(); } int main() { cout<<"The main .............."<<endl; //addInFile(); //添加結(jié)構(gòu)體 addCustomers("botao1",1,1); //myReadFile(); //讀取結(jié)構(gòu)體 return 0; }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
本文名稱:文件操作寫入和讀出結(jié)構(gòu)體--修改文件信息-創(chuàng)新互聯(lián)
瀏覽地址:http://www.rwnh.cn/article36/cssosg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、建站公司、移動(dòng)網(wǎng)站建設(shè)、虛擬主機(jī)、靜態(tài)網(wǎng)站、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容