C語言fopen函數了解

fopen()函數功能:open a file.

原型:FILE * fopen(const char * path,const char * mode);

需要#include<stdio.h>

返回值:文件順利打開後,指向該流的文件指針就會被返回。如果文件打開失敗則返回NULL,並把錯誤代碼存在errno 中。

一般打開文件會進行讀取或寫入操作,如果打開文件失敗,也就無法順利進行相應的讀寫操作,所以一般在調用fopen()之後要作錯誤判斷及處理。

參數path字符串包含欲打開的文件路徑及文件名,參數mode字符串則代表着流形態。

mode有以下幾種形態

"r" Opens for reading. If the file does not exist or cannot be found, thefopen call fails.

"w" Opens an empty file for writing. If the given file exists, its contents are destroyed.

"a" Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn't exist.

 

"r+" Opens for both reading and writing. (The file must exist.)"w+" Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed."a+" Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn't exist."b"  Open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed. 文本模式和二進制模式的區別:1.在windows系統中,文本模式下,文件以"\r\n"代表換行。若以文本模式打開文件,並用fputs等函數寫入換行符"\n"時,函數會自動在"\n"前面加上"\r"。即實際寫入文件的是"\r\n" 。2.在類Unix/Linux系統中文本模式下,文件以"\n"代表換行。所以Linux系統中在文本模式和二進制模式下並無區別。

DEMO示例1:

#include <stdio.h>  
#include <stdlib.h>  
#include <process.h>  
int main()  
{  
    char ch;  
    FILE *fp;  
    char fname[50];   // store file name  
    printf("input file name:");  
    scanf("%s",fname);  
    fp=fopen(fname,"r");  
    if (fp==NULL)  
    {  
        printf("error!\n");  
        getchar();  
        getchar();  
        exit(1);  
    }  
    //getc()用於在打開文件中獲取一個字符  
    while((ch=getc(fp))!=EOF)  
    {  
        putchar(ch);  
    }  
    printf("\n");  
    fclose(fp);  
    fp=NULL;  
    system("pause");  
    return 0;  
}  

DEMO2:

#include <stdio.h>  
FILE *stream,*stream2;  
int main(void)  
{  
    int numclosed;  
    if ((stream=fopen("data.txt","r"))==NULL)  
    {  
        printf("The file 'data.txt' was not opened\n");  
    }   
    else  
    {  
        printf("The file 'data.txt' was  opened\n");  
    }  
  
    if ((stream2=fopen("data2.txt","w+"))==NULL)  
    {  
        printf("The file 'data2.txt' was not opened\n");  
    }   
    else  
    {  
        printf("The file 'data2.txt' was  opened\n");  
    }  
    if (stream)  
    {  
        if (fclose(stream))  
        {  
            printf("The file data.txt was not opened\n");  
        }  
    }  
    numclosed = _fcloseall();  
    printf("Number of files close by _fcloseall %u\n",numclosed);  
    system("pause");  
    return 0;  
}  

文件操作時需要注意以下幾點:  1、在定義文件指針時,要將文件指針指向空;如 FILE *fp = NULL;

 

  2、文件操作完成後,需要將文件關閉,一定要注意,否則會造成文件所佔用內存泄露和在下次訪問文件時出現問題。

 

  3、文件關閉後,需要將文件指針指向空,這樣做會防止出現遊離指針,而對整個工程造成不必要的麻煩;如:fp = NULL;


【FROM MSDN && 百科】

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章