• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            小默

            [zz] File I/O

            http://www.csie.ntu.edu.tw/~cprog2003/downloads/Notes%20on%20C%20File%20I-O.htm

            File I/O

            The FILE type

            1. 當在C中想使用檔案時,就需要宣告FILE variable
            2. FILE variable是一個pointer,因它是一個指向檔案現在使用到哪裡的指標。 在比較底層的意義中(close to hardware),它是一個file descriptor。
            Example:
            FILE *myfile;
            

            在C中,實際上是使用Stream I/O的方式來存取資料。也就是說,當打開一個檔案後, OS那邊會將一部分的資料先讀起來在一個暫存的Buffer裡,然後FILE這個pointer就會去指向這個buffer, 每讀取一個字元時,它就會往前移動一個。同樣的,當我們在寫入的時候,當我們完成像是fprintf時, 它也是先寫入這個buffer中,直到這個buffer被flush或是寫出到device中,才會真正的做改變。

            這張圖的左邊就是device;右邊就是buffer。

            Associate the variable with a file

            1. Use fopen()
            2. Specify the file path and the mode
            3. 成功的話, fopen會return一個file pointer;否則, return NULL
            "r"open for reading; 假如檔案不存在,則失敗。
            "w"open or create for writing; 假如檔案存在,其現存的內容會被覆蓋。
            "a"open or create for writng; 看w的不同在於,它會接著現存的內容繼續做下去
            "r+"open for reading and writing; 檔案一定要存在
            "w+"open or create for reading and writing; 檔案不存在就開新檔案,存在就覆寫
            "a+"open or create for reading and writing; 不同處同上面a和w的差別

            FILE *fopen(char *name, char *mode)
            Example:
            FILE *myfile;
            myfile = fopen("input.txt", "r");

            Testing for EOF

            1. EOF是保留字,表示End Of File。
            2. 當想要檢查現在的file pointer是否已經只到檔案的結尾時,可以使用feof(file)
            3. 當真的已經是EOF時,return 0;否則,return non-zero
            Syntax:
            int feof( FILE *stream );
            Example: 
            if( feof( myfile ) )
            printf("End of file\n");

            Writing / Reading by single character

            1. To read in or write out text by char, use fgetc() and fputc()
            2. fgetc會return下一個在input stream中的char,若是已經EOF,則return EOF。而為什麼他要return int而不是char,則是因為EOF已經不在char的範圍內(不在0~255,為-1)。
            3. fputc則會return所寫入的char的值;假如發生錯誤的話,return EOF。
            Syntax:
            int fgetc( FILE *stream );
            int fputc( int c, FILE *stream );
            Example: 
            FILE *myfile, *myfile2;
            int c;
            myfile = fopen("in", "r");>
            myfile2 = fopen("out", "w");
            while( (c=fgetc(myfile)) != EOF)
            	fputc(c, myfile2);

            Writing / Reading by line of text

            1. To read in or write out text by line, use fgets() and fputs()
            2. fgets會return指向str的char pointer;假若發生錯誤或是遇到EOF時,returns NULL
            3. fputs return 0 on success and EOF on error.
            Syntax:
            char *fgets(char *str, int size, FILE *stream);
            int fputs(const char *str, FILE *stream);
            Example: 
            FILE *myfile, *myfile2;
            char tmp[80];
            myfile = fopen("in", "r");>
            myfile2 = fopen("out", "w");
            while( (fgets(tmp, 80, myfile)) != NULL)
            	fputs(tmp, myfile2);

            fprintf() and fscanf()

            1. Work like printf and scanf, except with files
            2. 跟上面fgets, fputs不同的是,這兩個function可以做formatted I/O
            Examples:
            fprintf(outputfile, "My age is %d\n", myAge);
            fscanf(inputfile, "%f", &floatVariable);
            
            

            Close the files

            1. 當在一個檔案的工作已經結束後,可以使用fclose(),使之前buffer的資料實際寫入。
            2. 因此當在對檔案的寫入結束後,最好還是用fclose將他關掉。
            3. 成功的話,return 0;否則,return EOF
            Syntax:
            int fclose( FILE *stream );

            A sample program

            假如你們還是不會用的話 ,可以套用(參考)下面的程式。
            #include 
            #define INFILE "input.txt"
            //將下面這個學號換成你自己的學號
            #define OUTFILE "R92922099"
            
            char *readin(FILE *);
            
            int main() {
                FILE *infile, *outfile;
                char *input;
            
            	/* 打開檔案 */
                if( (infile = fopen(INFILE, "r")) == NULL ) {
                    printf("can't open input file\n");
                    exit(1);								//假若失敗的話,就離開程式
                }else if( (outfile = fopen(OUTFILE, "w")) == NULL ) {
                    printf("can't open output file\n");
                    exit(1);								//假若失敗的話,就離開程式
                }
            	/* 用剛剛取得的file pointer來讀取檔案的內容的動作 */
                input = readin( infile );
            	/*
            		將你們如何處理從檔案中所讀到的資料result,
            		寫在這裡,或是在這裡呼叫function
            	*/
            
                fclose(infile);
                fclose(outfile);
            }
            
            //將檔案中所有的內容都讀取出來,用result指向這個資料,然後return這個pointer做處理
            char *readin(FILE *in) {
                char tmp[80];
                char *result="";
                while( fgets( tmp, 80, in)!=NULL ) {
                    asprintf(&result, "%s%s", result, tmp);
                }
                return result;
            }
            


            posted on 2011-03-24 17:17 小默 閱讀(598) 評論(0)  編輯 收藏 引用 所屬分類: Language

            導航

            統計

            留言簿(13)

            隨筆分類(287)

            隨筆檔案(289)

            漏洞

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            夜夜亚洲天天久久| 国产成人精品综合久久久| 欧美色综合久久久久久| 久久久久九国产精品| 天天综合久久一二三区| AV无码久久久久不卡蜜桃| 色欲av伊人久久大香线蕉影院| 久久国产免费观看精品| 亚洲国产一成久久精品国产成人综合 | 久久精品a亚洲国产v高清不卡| 久久精品国产99久久久| 久久夜色撩人精品国产| 亚洲综合熟女久久久30p| 日本久久久久久中文字幕| 香蕉久久AⅤ一区二区三区| 97久久精品午夜一区二区| 日本久久中文字幕| 91精品国产乱码久久久久久| 综合久久给合久久狠狠狠97色| 久久久91精品国产一区二区三区| 中文字幕久久精品| 精品久久久久中文字幕一区| 久久人人爽人人爽人人片AV不| 青青热久久国产久精品 | 久久久久久精品免费免费自慰| 国产午夜久久影院| 欧美黑人激情性久久| 久久人做人爽一区二区三区| 精品久久久久久无码人妻蜜桃| 97久久久久人妻精品专区| 亚洲精品无码久久久久去q| 日韩精品久久久久久久电影| 久久久久99精品成人片三人毛片| 国产精品一区二区久久| 久久精品无码一区二区无码| 伊人色综合久久天天人手人婷 | 久久成人精品| 国产精品VIDEOSSEX久久发布| 国产美女久久久| 久久国产精品-久久精品| 夜夜亚洲天天久久|