天天看點

檔案操作之打開檔案與讀寫檔案——C語言

一、fopen

函數原型:FILE *fopen( const char *filename, const char *mode );

傳回值:傳回值類型為FILE *,打開檔案成功傳回指向打開檔案的指針,打開檔案失敗傳回空指針(NULL)

代碼示例:

檔案操作之打開檔案與讀寫檔案——C語言
檔案操作之打開檔案與讀寫檔案——C語言

1 #include <stdio.h>
 2 
 3 void OpenFile(FILE **map);    //打開檔案
 4 void JudgeOpenSuc(FILE *judge);        //判斷檔案打開是否成功
 5 
 6 int main()
 7 {
 8     FILE *fp;
 9 
10     OpenFile(&fp);
11     JudgeOpenSuc(fp);
12 
13     return 0;
14 }
15 
16 void OpenFile(FILE **map)
17 {
18     (*map) = fopen("E:my.txt", "a+");
19 }
20 
21 void JudgeOpenSuc(FILE *judge)
22 {
23     if (judge != NULL)
24     {
25         printf("Open successfully\n");
26     }
27     else
28     {
29         printf("Open failure\n");
30     }
31 }      

View Code

二、fopen_s

函數原型:errno_t fopen_s( FILE** pFile, const char *filename, const char *mode );

傳回值:傳回值類型位errno_t,打開檔案成功傳回0,打開檔案失敗傳回非零

檔案操作之打開檔案與讀寫檔案——C語言
檔案操作之打開檔案與讀寫檔案——C語言
1 #include <stdio.h>
 2 
 3 const int SUC = 0;
 4 
 5 void OpenFile(FILE **map, errno_t *err);    //打開檔案
 6 void JudgeOpenSuc(errno_t err);        //判斷檔案打開是否成功
 7 
 8 int main()
 9 {
10     FILE *fp;
11     errno_t err;
12 
13     OpenFile(&fp, &err);
14     JudgeOpenSuc(err);
15 
16     return 0;
17 }
18 
19 void OpenFile(FILE **map, errno_t *err)
20 {
21     (*err) = fopen_s(map, "E:my.txt", "a+");
22 }
23 
24 void JudgeOpenSuc(errno_t err)
25 {
26     if (err == SUC)
27     {
28         printf("Open successfully\n");
29     }
30     else
31     {
32         printf("Open failure\n");
33     }
34 }      

三、_wfopen

函數原型:FILE *_wfopen( const wchar_t *filename, const wchar_t *mode );

檔案操作之打開檔案與讀寫檔案——C語言
檔案操作之打開檔案與讀寫檔案——C語言
1 #include <stdio.h>
 2 
 3 void OpenFile(FILE **map);    //打開檔案
 4 void JudgeOpenSuc(FILE *judge);        //判斷檔案打開是否成功
 5 
 6 int main()
 7 {
 8     FILE *fp;
 9 
10     OpenFile(&fp);
11     JudgeOpenSuc(fp);
12 
13     return 0;
14 }
15 
16 void OpenFile(FILE **map)
17 {
18     (*map) = _wfopen(L"E:my.txt", L"a+");
19 }
20 
21 void JudgeOpenSuc(FILE *judge)
22 {
23     if (judge != NULL)
24     {
25         printf("Open successfully\n");
26     }
27     else
28     {
29         printf("Open failure\n");
30     }
31 }      

四、_wfopen_s

函數原型:errno_t _wfopen_s( FILE** pFile, const wchar_t *filename, const wchar_t *mode );

檔案操作之打開檔案與讀寫檔案——C語言
檔案操作之打開檔案與讀寫檔案——C語言
1 #include <stdio.h>
 2 
 3 const int SUC = 0;
 4 
 5 void OpenFile(FILE **map, errno_t *err);    //打開檔案
 6 void JudgeOpenSuc(errno_t err);        //判斷檔案打開是否成功
 7 
 8 int main()
 9 {
10     FILE *fp;
11     errno_t err;
12 
13     OpenFile(&fp, &err);
14     JudgeOpenSuc(err);
15 
16     return 0;
17 }
18 
19 void OpenFile(FILE **map, errno_t *err)
20 {
21     (*err) = _wfopen_s(map, L"E:my.txt", L"a+");
22 }
23 
24 void JudgeOpenSuc(errno_t err)
25 {
26     if (err == SUC)
27     {
28         printf("Open successfully\n");
29     }
30     else
31     {
32         printf("Open failure\n");
33     }
34 }      

五、fscanf、fgetc、fgets、fscanf_s

fscanf()

函數原型:int fscanf (FILE *fp, const char *format, ……);

傳回值:參數清單中被成功讀取的參數個數

1 char ch;
2 fscanf(fp, "%c", &ch);      

fgetc()

函數原型:int fgetc( FILE *stream );

傳回值:讀取成功則以int形式讀取的字元對應的值(注意是int類型,如果用char類型的變量來接收傳回值可能會導緻資料截斷),讀取失敗傳回EOF

1 int ch;
2 ch = fgetc(fp);      

fgets()

函數原型:char *fgets( char *str, int numChars, FILE *stream );

傳回值:讀取成功時傳回字元數組首位址,也即 str;讀取失敗時傳回 NULL

char *p;
char ss[20];

p = fgets(ss, 20,fp);
if (p != NULL)
{
    printf("%s", ss);
}      

fscanf_s()

函數原型:int fscanf_s( FILE *stream, const char *format [, argument ]... );

傳回值:傳回成功讀取的參數數量

1 char ss[20];
2 int k;
3 
4 k = fscanf_s(fp, "%s", ss, _countof(ss));
5 printf("%s", ss);