文件操作之打開文件與讀寫文件——C語言


一、fopen

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

返回值:返回值類型為FILE *,打開文件成功返回指向打開文件的指針,打開文件失敗返回空指針(NULL)

代碼示例:

 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,打開文件失敗返回非零

代碼示例:

 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 }
View Code

 

三、_wfopen

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

返回值:返回值類型為FILE *,打開文件成功返回指向打開文件的指針,打開文件失敗返回空指針(NULL)

代碼示例:

 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 }
View Code

 

四、_wfopen_s

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

返回值:返回值類型位errno_t,打開文件成功返回0,打開文件失敗返回非零

代碼示例:

 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 }
View Code

 

五、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);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM