// file2.cpp : 定義控制臺應(yīng)用程序的入口點。 // #include "stdafx.h" #include <fstream> #include <iostream> using namespace std; struct Student{ int num; char name[20]; }; int addInFile() { ofstream outFile("botao.dat",ios::out|ios::binary); //定義文件輸出流 文件不存在時創(chuàng)建文件 //對文件打開錯誤時的操作 if(!outFile) { cout<<"The file open error!"<<endl; return 0; } else //文件正常打開時,進(jìn)行相應(yīng)的處理 { Student *s=new Student; cout<<"輸入學(xué)生學(xué)號:"; cin>>s->num; cout<<"輸入學(xué)生姓名:"; cin>>s->name; outFile.write((char*)s,sizeof(Student)); //文件輸出流向文件中寫入student信息 } outFile.close(); //關(guān)閉輸出流 return 1; } int myReadFile() { ifstream inFile("botao.dat",ios::in|ios::binary); //文件輸入流 將文件中的student信息讀出到屏幕上 //對文件打開錯誤時的操作 if(!inFile) { cout<<"The inFile open error!"<<endl; return 0; } else { Student *s=new Student; inFile.read((char*)s,sizeof(Student)); cout<<"學(xué)號:"<<s->num<<endl; cout<<"姓名:"<<s->name<<endl; } inFile.close(); //關(guān)閉輸入流 } int main() { cout<<"The main .............."<<endl; //addInFile(); //添加結(jié)構(gòu)體 myReadFile(); //讀取結(jié)構(gòu)體 return 0; }
新聞名稱:C++文件操作寫入和讀取結(jié)構(gòu)體類型
分享鏈接:http://www.rwnh.cn/article24/igjcce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、營銷型網(wǎng)站建設(shè)、面包屑導(dǎo)航、品牌網(wǎng)站制作、虛擬主機(jī)、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)