• <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 小默 閱讀(597) 評論(0)  編輯 收藏 引用 所屬分類: Language

            導航

            統計

            留言簿(13)

            隨筆分類(287)

            隨筆檔案(289)

            漏洞

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            午夜人妻久久久久久久久| 久久精品国产福利国产琪琪| 久久这里只有精品视频99| 亚洲午夜久久久| 久久精品蜜芽亚洲国产AV| 久久天天躁狠狠躁夜夜网站 | 久久影院综合精品| 国产精品成人久久久久三级午夜电影| 国产精品欧美亚洲韩国日本久久| 武侠古典久久婷婷狼人伊人| 久久人人爽人人爽人人AV| 亚洲国产精品久久66| 伊人久久大香线蕉亚洲五月天 | 久久―日本道色综合久久| 热久久最新网站获取| 色综合久久88色综合天天| 国产色综合久久无码有码| 久久精品无码专区免费| 国产婷婷成人久久Av免费高清| 久久综合一区二区无码| 久久精品成人国产午夜| 色综合久久久久综合体桃花网| 久久精品亚洲男人的天堂| 久久精品视频网| 人人狠狠综合久久88成人| 欧美亚洲国产精品久久| 亚洲国产精品成人AV无码久久综合影院| 国产国产成人精品久久| 丰满少妇高潮惨叫久久久| 亚洲av成人无码久久精品| 一本色道久久综合狠狠躁| 久久天天躁狠狠躁夜夜躁2014| 亚洲欧美日韩精品久久亚洲区| 久久亚洲av无码精品浪潮| 人妻少妇精品久久| 热综合一本伊人久久精品| 欧美亚洲日本久久精品| 色综合久久88色综合天天 | 久久受www免费人成_看片中文| 亚洲精品午夜国产va久久| 人妻无码精品久久亚瑟影视 |