1 / 14

基礎程式設計 上機課程

基礎程式設計 上機課程. day:2014.1.3 洪從 罡. structure – review(1). 定 義 、宣告方式. typedef struct myStruct { int x, y ; char str [100] ; } ; myStruct test ;. structure – review(2). typedef struct myst { int x,y ; myNode *next ; } ; myst *p, q ; p = ( myst *) malloc ( sizeof ( myst )) ;

Download Presentation

基礎程式設計 上機課程

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 基礎程式設計 上機課程 day:2014.1.3 洪從罡

  2. structure – review(1) • 定義、宣告方式 typedefstructmyStruct { int x, y ; char str[100] ; } ; myStructtest ;

  3. structure – review(2) typedefstructmyst{ intx,y ; myNode *next ; } ; myst *p, q ; p = (myst*)malloc(sizeof(myst)) ; p->x = 1 ; p->y = 2 ; q.x = 1 ; q.y = 2 ; • . ->使用時機的不同

  4. FILE • 一個指向檔案開頭的檔案指標 • 做為fopen(),freopen,fclose(),fprintf()等檔案函數的參數型態 • 使用方式: FILE *myfile ; 注意此型態皆為大寫

  5. fopen() • fopen( ) 用於開啟檔案,檔案在使用前需先經過開啟動作 • 格式如下:FILE *fopen(char *filename, char *mode); • filename: 你想要開啟的檔案名稱,需要和執行檔放在同目錄下 • mode : • r :開啟一個文字檔(text) • w : 開啟一個文字檔(text),供程式將資料寫入此檔案內 • a : 開啟一個文字檔(text),供程式將資料寫入此檔案的末端 • rb : 開啟一個二元檔(binary) • wb : 開啟一個二元檔,供程式將資料寫入此檔案內 • ab :開啟一個二元檔(binary),供程式將資料寫入此檔案末端

  6. fclose( ) • 用於關閉檔案,如果fclose( )執行失敗,它的傳回值是非零值 if(fclose(myfile)!=0) { printf(“Close File Fail !!!\n”) ; }

  7. fprintf( ), fscanf( ) • fprintf() : 將資料寫入某檔案內 • fprintf( fp , "……." , ………) ; //same as printf • fscanf() :從某個檔案讀取資料 • fscanf( fp , "……." , ………) ; // same as scanf

  8. fread(), fwrite() • 使用在讀寫binary檔的時候 • fread(buf, size, count, fp); • buf:存放讀取Data的位置 size:讀取一組data的大小 • count:讀取幾組datafp:FILEpointer • fwrite(buf,size,count,fp); • buf:存放讀取Data的位置 size:寫入一組data的大小 • count:寫入幾組datafp:FILEpointer

  9. feof() • feof(p); // p FILE pointer • 當FILEpointerp指向檔案結尾EOF時,此函式會回傳true • 可用來進行檔案結束判斷

  10. example (1) FILE *in, *out ; inti, a[1000] ; in = fopen(“in.txt”, “r”) ; out = fopen(“out.txt”, “r”) ; if(!in || !out) { printf(“File open fail !!!\n”) ; return 0 ; } while(fscanf(in, “%d”, &a[i])!=EOF) { fprintf(out, “ID %d : %d -> %lf\n”, i, a[i], sqrt((double)a[i])*10) ; } fclose(in) ; fclose(out) ;

  11. example (2)-fwrite() typedefstruct Character{ char sex; //M: male F:female int age ; char name[100] ; } ; FILE *p= fopen("data.dat", "wb+") ; struct Character c1 = {'F', 900, "aRcUIeD"} ; struct Character c2 = {'M', 15, "kiRayAMatO"} ; struct Character c3 = {'F', 14, "mIsSakAmiKotO"} ; struct Character c3 = {'F', 17, "hitagi"} ; struct Character c3 = {'F', 18, "Christina"} ; fwrite(&c1, sizeof(struct Character), 1, p) ; fwrite(&c2, sizeof(struct Character), 1, p) ; fwrite(&c3, sizeof(struct Character), 1, p) ; fwrite(&c4, sizeof(struct Character), 1, p) ; fwrite(&c5, sizeof(struct Character), 1, p) ; fclose(p) ;

  12. example (3)-fread() typedefstruct Character{ char sex ; //M: male F:female int age ; char name[100] ; }; FILE *p = fopen("data.dat", "rb") ; struct Character c = {0,0,""} ; while(1) { fread(&c, sizeof(struct Character), 1, p) ; if(feof(p)) break ; printf("%s. %s is %d years old.\n" , c.name , c.sex=='M' ? "He" : "She" , c.age) ; } fclose(p) ;

  13. practice - 1 • 請寫一隻程式讀取一個TXT檔 • 將檔案中所有的英文字母大小寫互換 • 並把所有的符號改成 ‘#‘,空白換行除外 • 數字則維持不變 • sample input: 1_1.in 1_2.in 1_3.in

  14. practice - 2 • 請寫一隻程式以binary的方式讀取一個檔案, • 每次讀取1Byte,讀取後將該值以unsigned int的方式存起來 • 統計有哪些數值有出現過,並且計算出現了幾次 • 最後把結果輸出成檔案 • 注意印出的順序必須依照該數值出現的順序印出 • sample input: 2_1.in 2_2.in 2_3.in • sample output: 2_1.out 2_2.out 2_3.out .jpg .mp3 .mp4

More Related