首页 > 编程知识 正文

r语言fread函数用法,r语言rep函数

时间:2023-05-06 08:51:32 阅读:223257 作者:203

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 textfp = fopen("fileA","r");//"r" = "rt", 因为默认打开textfp = fopen("fileA","rb");//打开一个二进制文件,文件必须存在,只允许读fp = fopen("fileA","w");//新建一个文本文件,若存在原有文件则清空,只允许写fp = fopen("fileA","wt");//"w" = "wt", 因为默认打开textfp = fopen("fileA","wb");//新建一个二进制文件,若存在原有文件则清空,只允许写

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

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

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

File *fp;fp = fopen("fileA","r+");//打开文本文件,文件必须存在,允许读写fp = fopen("fileA","rt+");//"r+" = "rt+", 因为默认打开textfp = fopen("fileA","w+");//创建文本文件, 若已存在则清空文件内容,允许读写fp = fopen("fileA","rt+");//"w+" = "wt+", 因为默认打开textfp = fopen("fileA","rb+");//打开二进制文件,文件必须存在,允许读写fp = fopen("fileA","wb+");//创建二进制文件, 若已存在则清空文件内容,允许读写fp = fopen("fileA","a+");//打开或新建一个文本文件,可以读,但只允许在文件末尾追写fp = fopen("fileA","at+");//"a+" = "at+", 因为默认打开textfp = 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 fileint arr[10];fread(arr, sizeof(arr), 1, fp);//Example 2: Reading the structure variablestruct 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 filen"); exit(1); } printf("Testing fread() function: nn"); //循环读取,会自动往后读,而不会反复读取文件的最前面的部分 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 nn", emp.salary); } fclose(fp); return 0;}

输出结果:

Testing fread() function: Name: BobDesignation: ManagerAge: 29Salary: 34000.00 Name: JakeDesignation: DeveloperAge: 34Salary: 56000.00

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。