文件操作之打开文件与读写文件——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);
posted @ 2019-07-15 21:13  Luv3  阅读(2760)  评论(0编辑  收藏  举报