C 實踐參考 用二進位制檔案處理學生成績

2021-09-23 18:06:44 字數 1612 閱讀 8741

[參考解答]

#include 

#include

#include

#include

using

namespace

std;

//(一)定義學生類

class student

; student(int n, string nam, double c, double m, double e):num(n),name(nam),cpp(c),math(m),english(e)

void set_value(int n,string nam, double c, double m, double e);

string get_name()

double get_cpp()

double get_math()

double get_english()

double get_total()

void set_cpp(double c)

void set_math(double m)

void set_english(double e)

void set_total(double t)

friend ostream& operator

<<(ostream&, student&);

private:

int num;

string name;

double cpp;

double math;

double english;

double total;

};void student::set_value(int n,string nam, double c, double m, double e)

ostream& operator

<<(ostream& out, student& s)

int main( )

for(i=0;i<100;i++)

infile.close();

//(三)將所有資料儲存到乙個二進位制檔案binary_score.dat中

ofstream outfile("binary_score.dat",ios::out|ios::binary);

if(!outfile)

for(i=0;i<100;i++)

//(三)最後在檔案中寫入你自己的各科成績

cout

<<"輸入你自己的資訊:";

cin>>n>>sname>>scpp>>smath>>senglish;

student me(n,sname, scpp, smath, senglish);

outfile.write((char*)&me, sizeof(me));

outfile.close();

//(四)為驗證輸出檔案正確,再將binary_score.dat中的記錄逐一讀出到學生物件中並輸出檢視。

student s;

ifstream infile2("binary_score.dat",ios::in|ios::binary);

if(!infile2)

while(true)

《本文完》

用二進位制檔案處理學生成績

輸入 檔名稱 sum123.cpp 作 者 林海雲 完成日期 2015年8月19日 版 本 號 v2.0 問題描述 1 定義學生類,其中包含學號 姓名 c 課 高數和英語成績及 總分資料成員,成員函式根據需要確定。2 讀入學生的成績,並求出總分,用物件陣列進行儲存。ascii檔案score.dat中...

專案 用二進位制檔案處理學生成績

專案 用二進位制檔案處理學生成績 1 定義學生類,其中包含學號 姓名 c 課 高數和英語成績及總分資料成員,成員函式根據需要確定。2 讀入學生的成績,並求出總分,用物件陣列進行儲存。ascii檔案score.dat中儲存的是100名學生的學號 姓名和c 課 高數和英語成績。3 將所有資料儲存到乙個二...

用 C 讀取二進位制檔案

當想到所有檔案都轉換為 xml時,確實是一件好事。但是,這並非事實。仍舊還有大量的檔案格式不是xml,甚至也不是ascii。二進位制檔案仍然在網路中傳播,儲存在磁碟上,在應用程式之間傳遞。相比之下,在處理這些問題方面,它們比文字檔案顯得更有效率些。在 c 和 c 中,讀取二進位制檔案還是很容易的。除...