C语言 fopen和fread函数解析

fopen函数

fopen()函数可以用来打开文件,写法如下,第一个参数是文件名路径,第二个参数的是打开文件的方式:

FILE *fp1;
fp1 = fopen("file a","r");//r代表read,表示只读
FILE *fp2;
fp2 = fopen("c:\\hzk16","rb");//读取c盘的hzk16文件,r代表read,b代表binary,表示只读二进制文件

一共有六种符号:

r: read 读
w: write 写
t: text 文本文件
b: binary 二进制文件
+: both read and write 读写
a: append 末尾追写

以下是最基本的只读只写的操作

FILE *fp;
fp = fopen("fileA","rt");//打开一个文本文件,文件必须存在,只允许读,rt: read text
fp = fopen("fileA","r");//"r" = "rt", 因为默认打开text

fp = fopen("fileA","rb");//打开一个二进制文件,文件必须存在,只允许读

fp = fopen("fileA","w");//新建一个文本文件,若存在原有文件则清空,只允许写
fp = fopen("fileA","wt");//"w" = "wt", 因为默认打开text

fp = fopen("fileA","wb");//新建一个二进制文件,若存在原有文件则清空,只允许写

如果只允许在文件的末尾进行写入操作,可以用以下方式:

FILE *fp;
fp = fopen("fileA","a");//打开或新建一个文本文件,只允许在文件末尾追写
fp = fopen("fileA","at");//"a" = "at", 因为默认打开text

fp = fopen("fileA","ab");//打开或新建一个二进制文件,只允许在文件末尾追写

+字符,可以实现读写操作。

File *fp;
fp = fopen("fileA","r+");//打开文本文件,文件必须存在,允许读写
fp = fopen("fileA","rt+");//"r+" = "rt+", 因为默认打开text

fp = fopen("fileA","w+");//创建文本文件, 若已存在则清空文件内容,允许读写
fp = fopen("fileA","rt+");//"w+" = "wt+", 因为默认打开text

fp = fopen("fileA","rb+");//打开二进制文件,文件必须存在,允许读写
fp = fopen("fileA","wb+");//创建二进制文件, 若已存在则清空文件内容,允许读写

fp = fopen("fileA","a+");//打开或新建一个文本文件,可以读,但只允许在文件末尾追写
fp = fopen("fileA","at+");//"a+" = "at+", 因为默认打开text
fp = fopen("fileA","ab+");//打开或新建一个二进制文件,可以读,但只允许在文件末尾追写

特别注意是:

  • 带字符 r 的操作,必须有该文件存在,否则会返回空。
  • 带字符 w 的操作,如果有原文件,该操作会删除原文件,再新建文件,如果没有原文件则直接新建文件。
  • 带字符 a 的操作,如果有文件则会打开,没有就会新建,不会清空内容,但只能在尾部写

从上面看出,如果想要实现这样的操作:如果有文件就读文件,没文件就创建文件,而且保证文件是可读可写的,这个时候好像没有任何一个单一的fopen能满足需求。
如果用fopen("name", "w+"),那么如果有文件,也会被覆盖掉,
如果用fopen("name", "r+"),那么如果没有文件,会返回false,所以实现上述功能需要结合两个函数,如下所示:

if ((f = fopen(filename, "r+") == 0) //若存在文件,则直接打开
    f = fopen(filename, "w+"); //若不存在文件,则创建文件
if (f == 0)
    ...report error...

fread函数

参考链接:https://overiq.com/c-programming-101/fread-function-in-c/
The syntax of fread() function is this:

fread() function is the complementary of fwrite() function. fread() function is commonly used to read binary data. It accepts the same arguments as fwrite() function does.

Syntax: size_t fread(void *ptr, size_t size, size_t n, FILE *fp);

The ptr is the starting address of the memory block where data will be stored after reading from the file. The function reads n items from the file where each item occupies the number of bytes specified in the second argument. On success, it reads n items from the file and returns n. On error or end of the file, it returns a number less than n.

举两个简单的例子:

//Example 1: Reading an array from the file
int arr[10];
fread(arr, sizeof(arr), 1, fp);

//Example 2: Reading the structure variable
struct student
{
    char name[10];
    int roll;
    float marks;
};
 
struct student student_1;
 
fread(&student_1, sizeof(student_1), 1, fp);

关于fread最值得注意的一点,fread读取文件时,会自动保留当前的读取位置,也就是说,不用自己去管读取的位置在哪个字节,读取了对应文件的部分后,会自动继续往下读取,关于这里点,可以参考https://stackoverflow.com/questions/10696845/does-fread-move-the-file-pointer,具体举个例子。

#include<stdio.h>
#include<stdlib.h>
 
struct employee
{
    char name[50];
    char designation[50];
    int age;
    float salary
} emp;
 
int main()
{
    FILE *fp;
    fp = fopen("employee.txt", "rb");
 
    if(fp == NULL)
    {
        printf("Error opening file\n");
        exit(1);
    }
 
    printf("Testing fread() function: \n\n");
    
 	//循环读取,会自动往后读,而不会反复读取文件的最前面的部分
    while( fread(&emp, sizeof(emp), 1, fp) == 1 )
    {
        printf("Name: %s \n", emp.name);
        printf("Designation: %s \n", emp.designation);
        printf("Age: %d \n", emp.age);
        printf("Salary: %.2f \n\n", emp.salary);
    }
 
    fclose(fp);
    return 0;
}

输出结果:

Testing fread() function:
 
Name: Bob
Designation: Manager
Age: 29
Salary: 34000.00
 
Name: Jake
Designation: Developer
Age: 34
Salary: 56000.00
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值