【c++】C语言文件操作函数之fwrite()和fread()

size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );


Read block of data from stream
Reads an array of count elements, each one with a size of size bytes, from the stream and stores them in the block of memory specified by ptr.
The postion indicator of the stream is advanced by the total amount of bytes read.
The total amount of bytes read if successful is (size * count).

从数据流里读一块数据

从流中读取一个count元素数组,每个元素的大小都是字节,并将它们存储在ptr指定的内存块中。
流的位置指示器由读取的总字节数推进。
如果成功,则读取的字节总数为count*size

ptr

Pointer to a block of memory with a minimum size of (size*count) bytes.

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 that specifies an input stream.

Return Value

The total number of elements successfully read is returned as a size_t object, which is an integral data type.
If this number differs from the count parameter, either an error occured or the End Of File was reached.
You can use either ferror or feof to check whether an error happened or the End-of-File was reached.

size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream ); 

Write block of data to stream

Writes an array of count elements, each one with a size of size bytes, from the block of memory pointed by ptr to the current position in the stream.
The postion indicator of the stream is advanced by the total amount of bytes written.
The total amount of bytes written is (size * count).

写一块数据到流里

从ptr指向的内存块到流中的当前位置写入一个count元素数组,每个元素的大小都是字节大小。
流的位置指示器由写入的总字节数推进。
写入的字节总数是count*size。

Parameters

ptr

Pointer to the array of elements to be written.

size

Size in bytes of each element to be written.

count

Number of elements, each one with a size of size bytes.

stream

Pointer to a FILE object that specifies an output stream.

 

Return Value

The total number of elements successfully written is returned as a size_t object, which is an integral data type.
If this number differs from the count parameter, it indicates an error.

 举个栗子:注意用二进制方式打开和读写的文件,只能用fread()和fwrite()来操作。

#include<stdio.h>
#include<stdlib.h>
//对同一个文件的读写操作
//fseek,ftell
//
int main()
{
	char* buf=NULL;
	FILE *wp = NULL;
	FILE *rp = NULL;
	int a = 20;
	//char str[20] = "1";
	wp = fopen("C:\\Users\\Z7M\\Desktop\\2.txt", "wb");
	if (NULL == wp)
	{
		printf("打开错误\n");
	}
	rp= fopen("C:\\Users\\Z7M\\Desktop\\1.txt", "rb");
	if (NULL == rp)
	{
		printf("打开错误\n");
	}
	/*wp = fopen("C:\\Users\\Z7M\\Desktop\\2.txt", "rb+");
	if (NULL == wp)
	{
		printf("打开错误\n");
	}*/
	//fread(&buf, 4, 1, rp);//与rb匹配,返回值表示正确读取的次数
	//fwrite(&buf, 4, 1, wp);//与wb匹配
	//rewind(wp);
	fseek(rp, 0, 2);
	a=ftell(rp);
	rewind(rp);
	printf("%d", a);
	buf = (char*)malloc(a + 1);
	fread(buf, a, 1, rp);
	fwrite(buf, a, 1, wp);
	fclose(wp);
	wp = NULL;
	fclose(rp);
	rp = NULL;
	free(buf);
	buf = NULL;
	//FILE *rp = NULL;
	//rp = fopen("C:\\Users\\Z7M\\Desktop\\4.txt", "rb");
	//if (!rp)
	//{
	//	perror("fail");//判断是打开失败还是读到中间失败
	//}
	//else if (feof(rp))//判断是不是因为读到文件尾而结束
	//	puts("End of file reached successfully");
	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值