C语言 FileStreaming fwrite&fread

格式化IO

方法描述
fprintfWrite formatted data to stream (function ) 向文件格式化写入
fscanfRead formatted data from stream (function ) 向文件格式化读出
sprintfWrite formatted data to string (function ) 向C字符串读出
sscanfRead formatted data from string (function ) 向C字符串写入
printfPrint formatted data to stdout (function ) 向标准输出给石化读出
scanfRead formatted data from stdin (function ) 向标准输入格式化写入

字符字符串IO

参数描述
fgetc(函数实现)Get character from stream (function ),getc(宏实现)
fputc(函数实现)Write character to stream (function ) putc(宏实现)
fgetsGet string from stream (function )
fputsWrite string to stream (function )
getcharGet character from stdin (function )
putcharWrite character to stdout (function )
getsGet string from stdin (function )
putsWrite string to stdout (function )

直接IO(二进制IO)

  1. 二进制读写,在读文件的时候加b
方法描述
freadRead block of data from stream (function )
fwriteWrite block of data to stream (function )

fread

size_t fread ( void * ptr, size_t size, size_t count, FILE * stream ) Read block of data from stream

  1. fread()──从fp所指向文件的当前位置开始,一次读入size个字节,重复count次,并将读入的数据存放到从buffer开始的内存中;
  2. 读写的是数据块,既size_t size
    参数|描述
    –|--
    ptr|block of memory with a size of at least (sizecount) bytes,converted to a void
    size|Size, in bytes, of each element to be read.
    count|Number of elements, each one with a size of size bytes.
    stream|Pointer to a FILE object
返回描述
成功返回count
#pragma warning(disable:4996)

#include<stdio.h>
#include<stdlib.h>

int main()
{
	FILE *fp = fopen("test.txt", "r");
	char arr[5] = { 0,0,0,0,0 }; //一定要初始化数组,不然会很'烫'(声明数组时,内存空间可能被其他程序修改过)
	fread(arr, 2, 2, fp);
	printf("%s\n",arr);
	fclose(fp);
	system("pause");
	return(0);
}

fwrite

和fread对应

int 类型读写

#pragma warning(disable:4996)

#include <stdio.h>
#include <stdlib.h>
int main()
{
	FILE * pFile = fopen("test.txt", "wb+");

	int wbuffer[] = { 1 };
	fwrite(wbuffer, sizeof(int), 1, pFile);

	rewind(pFile);// 此时文件position已经在1字节后
	int rbuffer[] = { 0 };
	fread(rbuffer, sizeof(int), 1, pFile);

	printf("%d\n", rbuffer[0]);
	system("pause");
	fclose(pFile);
	return 0;
}

结构体读写

#pragma warning(disable:4996)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
	int age;
	char name[10];
}person;


int main()
{
	FILE * pFile = fopen("test.txt", "wb+");
	
	person wper;
	wper.age = 10;
	strcpy(wper.name, "wang");// 字符串数组
	fwrite(&wper, sizeof(person), 1, pFile);
	
	rewind(pFile);// 此时文件position已经在1字节后
	
	person rper;
	fread(&rper, sizeof(person), 1, pFile);
	printf("%d %s\n", rper.age, rper.name);
	system("pause");
}

参考:
http://www.cplusplus.com/reference/cstdio/
http://www.gnu.org/software/libc/manual/html_node/I_002fO-on-Streams.html#I_002fO-on-Streams

fwritefread函数是C语言中用于文件读写的函数,它们都是标准库函数,定义在头文件stdio.h中。 fwrite函数的原型如下: ```c size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream); ``` 参数说明: - ptr:要写入的数据的指针。 - size:每个数据项的大小(单位为字节)。 - count:要写入的数据项的数量。 - stream:文件指针。 返回值为成功写入的数据项的数量。 fwrite函数用于将数据写入文件中,它按照指定的大小和数量将数据写入文件中。如果写入成功,函数返回成功写入的数据项的数量,否则返回0。 例如,下面的代码将一个整数数组写入文件中: ```c #include <stdio.h> int main() { int arr[] = {1, 2, 3, 4, 5}; FILE *fp = fopen("data.txt", "wb"); if (fp == NULL) { printf("Failed to open file.\n"); return 1; } fwrite(arr, sizeof(int), 5, fp); fclose(fp); return 0; } ``` fread函数的原型如下: ```c size_t fread(void *ptr, size_t size, size_t count, FILE *stream); ``` 参数说明: - ptr:要读取数据的缓冲区的指针。 - size:每个数据项的大小(单位为字节)。 - count:要读取的数据项的数量。 - stream:文件指针。 返回值为实际读取的数据项的数量。 fread函数用于从文件中读取数据,它按照指定的大小和数量从文件中读取数据。如果读取成功,函数返回实际读取的数据项的数量,否则返回0。 例如,下面的代码从文件中读取一个整数数组: ```c #include <stdio.h> int main() { int arr[5]; FILE *fp = fopen("data.txt", "rb"); if (fp == NULL) { printf("Failed to open file.\n"); return 1; } fread(arr, sizeof(int), 5, fp); fclose(fp); for (int i = 0; i < 5; i++) { printf("%d\n", arr[i]); } return 0; } ``` 以上代码会输出以下结果: ``` 1 2 3 4 5 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值