實驗7 檔案

2022-01-23 17:37:28 字數 3278 閱讀 2727

實驗任務1、2 自行實踐完畢

實驗任務3

//

從文字資料檔案file1.dat中讀入資料,按成績從高到低排序,將排序結果輸出到螢幕上,同時以文字方式存入檔案file3.dat中。

#include #include

#define n 10

//定義乙個結構體型別stu

typedef struct

student stu;

void sort(stu *pst, int n); //

函式宣告

intmain ()

//從fin指向的資料檔案file1.dat中讀取資料到結構體陣列st

for(i=0; i)

fscanf(fin,

"%d %s %d

", &st[i].num, st[i].name, &st[i].score);

fclose(fin);

//關閉fin指向的檔案file1.dat

//呼叫函式scort()對陣列st中資料,按分數由高到低排序

sort(st, n);

//以寫方式開啟/建立文字檔案file3.dat

fout = fopen("

file3.dat

", "w"

);

if( !fout )

//將排序後的陣列st中資料輸出到螢幕,同時,也寫入檔案file3.dat

for(i=0; i)

fclose(fout);

//關閉fout指向的檔案file3.dat

return0;

}//排序演算法:選擇排序演算法

//說明:選擇排序演算法是確定的,但其具體實現方式和細節確實靈活多樣的

//本例中,選擇排序演算法的函式體中,都是通過指標變數操作的

//情節和**體會和理解,做到理解演算法本質,才能應對和理解靈活多樣的實現形式

void sort(stu *pst, int

n) }

/*執行程式,螢幕上確實正確輸出了按分數由高到低排序的資訊。

同時,在當前路徑下,生成了文字檔案file3.dat

使用記事本程式開啟檔案file3.dat,裡面的資料正確並且直觀可讀

實驗任務4

//

從文字資料檔案file1.dat中讀入資料,按成績由高到低排序,並將排序結果輸出到螢幕上,同時,也以二進位制方式存入檔案file4.dat中。

#include #include

#define n 10

//定義乙個結構體型別stu

typedef struct

student stu;

void sort(stu *pst, int n); //

函式宣告

intmain ()

//從fin指向的資料檔案file1.dat中讀取資料到結構體陣列st

for(i=0; i)

fscanf(fin,

"%d %s %d

", &st[i].num, st[i].name, &st[i].score);

fclose(fin);

//關閉fin指向的檔案file1.dat

//呼叫函式sort()對陣列st中資料,按分數由高到低排序

sort(st, n);

//以寫方式開啟/建立二進位制檔案file4.dat

fout = fopen("

file4.dat

", "wb"

);

if( !fout )

//將排序後的陣列st中資料輸出到螢幕

for(i=0; i)

printf(

"%-6d%-10s%3d\n

", st[i].num, st[i].name, st[i].score);

//將排序後的陣列st中資料寫到二進位制檔案file4.dat

for(i=0; i)

fwrite(st,

sizeof(stu), n, fout); //

將從位址st開始的sizeof(stu)xn個位元組資訊寫入fout指向的檔案file4.dat中

fclose(fout); //

關閉fout指向的檔案file4.dat

/*//子任務2

file *pp;

pp = fopen("file4.dat", "rb");

printf("\n");

fread(s, sizeof(stu), n, pp);

for(i=0; i*/

return

0;

} //

函式功能描述:對pst指向的n個stu結構體資料進行排序,按成績資料項由高到低排序

//排序演算法:選擇排序演算法

void sort(stu *pst, int

n) }

/*子任務1中的問題

執行程式,螢幕上確實正確輸出了按分數由高到低排序的資訊。

同時,在當前路徑下,生成了二進位制檔案file4.dat

使用記事本程式開啟檔案file.dat,裡面的資料為一行亂碼,不可讀

*/

實驗任務5

#include #include 

const

int n = 10;//

定義結構體型別struct student,並定義其別名為stu

typedef struct

student stu;

//函式宣告

void input(stu s, int

n);void output(stu s, int

n);void process(stu s, int

n);int

main ()

//void input(stu s, int

n)

//輸出考生完整資訊:准考證號,姓名,客觀題得分,操作題得分,總分,等級

//不僅輸出到螢幕上,還寫到文字檔案result.txt中

void output(stu s, int

n)

//對考生資訊進行處理:計算總分,排序,確定等級

void process(stu s, int

n)

for(i=0; i)

}

實驗7 檔案

實驗任務3 執行程式,觀察到在螢幕上正確輸出了按分數由高到低排序的資訊,唯一不足的事對於漢字的解碼不正確導致輸出的學生姓名成了亂碼。同時,在當前格式下,生成了文字檔案file3.dat 嘗試用記事本程式開啟檔案file3.dat,觀察到裡面的資料資訊是正確的,並且是直觀可讀的 實驗任務4.1 執行程...

(實驗2)檔案IO實驗

1 將原始檔複製到另外乙個檔案 將test1.text複製成test2.txt test linux test mycp home test1.txt usr test2.txt 2 將原始檔複製到另外乙個目錄 將test1.txt複製到 tmp目錄 test linux test mycp hom...

實驗3 檔案操作

一 實驗目的與要求 學會使用檔案操作函式實現對檔案開啟 關閉 讀 寫等操作。學會對資料檔案進行簡單的操作。深入理解 c 的輸入輸出的含義及其實現方法。掌握標準輸入輸出流的應用。二 實驗過程 2 用隨機函式根據文字檔案的記錄數量生成乙個隨機數 3 根據這個隨機數,從所讀取的記錄中找到對應的記錄,並輸出...