Bootstrap

linux——标准c语言fopen、fwrite、fread补充

#include <stdio.h>
#include <string.h>

int main()
{
	FILE *fp;
	char *str ="yangzeruihenshuai";
	char readbuf[128]={0};
	
	//FILE *fopen(const char *path, const char *mode);
	fp=fopen("./chen.txt","w+");


	//size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
	int nwrite=fwrite(str,sizeof(char),strlen(str),fp);
	//fwrite(str,sizeof(char)*strlen(str),1,fp);
	
	fseek(fp,0,SEEK_SET);
	//size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
	int nread=fread(readbuf,sizeof(char),strlen(str),fp);

	printf("read data:%s\n",readbuf);
	printf("nwrite=%d,nread=%d\n",nwrite,nread);

	return 0;
}

输出结果nwrite=17,nread=17,

从结果可以看出,函数返回值fwrite、fread函数的第3个参数

如果读写全部,只读写一次

则输出:nwrite=1,nread=1

如果全部写入,写入一次;读取全部读出,读取100次

结果为:nwrite=1,nread=1

nread输出结果不为100

;