函数原型
(1)size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
其中,ptr:指向保存结果的指针;size:每个数据类型的大小;count:数据的个数;stream:文件指针
函数返回读取数据的个数。
(2)size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
其中,ptr:指向保存数据的指针;size:每个数据类型的大小;count:数据的个数;stream:文件指针
函数返回写入数据的个数。
注意事项:
(1)写操作fwrite()后必须关闭流fclose()。
(2)不关闭流的情况下,每次读或写数据后,文件指针都会指向下一个待写或者读数据位置的指针。
写int数据到文件:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
FILE * pFile;
int buffer[] = {1, 2, 3, 4};
if((pFile = fopen ("myfile.txt", "wb"))==NULL)
{
printf("cant open the file");
exit(0);
}
//可以写多个连续的数据(这里一次写4个)
fwrite (buffer , sizeof(int), 4, pFile);
fclose (pFile);
return 0;
}
读int:
#include <stdio.h>
#include <stdlib.h>
int main () {
FILE * fp;
int buffer[4];
if((fp=fopen("myfile.txt","rb"))==NULL)
{
printf("cant open the file");
exit(0);
}
if(fread(buffer,sizeof(int),4,fp)!=4) //可以一次读取
{
printf("file read error\n");
exit(0);
}
for(int i=0;i<4;i++)
printf("%d\n",buffer[i]);
return 0;
}
写结构体数据到文件:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct{
int age;
char name[30];
}people;
int main ()
{
FILE * pFile;
int i;
people per[3];
per[0].age=20;strcpy(per[0].name,"li");
per[1].age=18;strcpy(per[1].name,"wang");
per[2].age=21;strcpy(per[2].name,"zhang");
if((pFile = fopen ("myfile.txt", "wb"))==NULL)
{
printf("cant open the file");
exit(0);
}
for(i=0;i<3;i++)
{
if(fwrite(&per[i],sizeof(people),1,pFile)!=1)
printf("file write error\n");
}
fclose (pFile);
return 0;
}
读结构提:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct{
int age;
char name[30];
}people;
int main () {
FILE * fp;
people per;
if((fp=fopen("myfile.txt","rb"))==NULL)
{
printf("cant open the file");
exit(0);
}
while(fread(&per,sizeof(people),1,fp)==1) //如果读到数据,就显示;否则退出
{
printf("%d %s\n",per.age,per.name);
}
return 0;
}
写char类型数据;4096个:
#include "stdafx.h"
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define DATA_SIZE 1024
int main()
{
unsigned char *dataPtr = NULL;
dataPtr = (unsigned char *)malloc(sizeof(int)*DATA_SIZE); //申请的区域是4096个char,即1024个字的区域
for(unsigned int i=0;i<DATA_SIZE;i++)
{
dataPtr[i] = i; //初始化缓存区
}
FILE *fp = fopen("F:\\Labwindows cvi\\test.txt","ab+");
fwrite(dataPtr,sizeof(char),DATA_SIZE*sizeof(int),fp);
fclose(fp);
free(dataPtr);
system("pause");
return 0;
}
用malloc函数申请区域时是申请的一片char*区域,通过强制类型转换后可装unsigned int 数据
#include "stdafx.h"
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define DATA_SIZE 1024
int main()
{
unsigned char *dataPtr = NULL;
unsigned int *Ptr = NULL;
dataPtr = (unsigned char *)malloc(sizeof(int)*DATA_SIZE);
Ptr = (unsigned int *) dataPtr;
for(unsigned int i=0;i<DATA_SIZE;i++)
{
Ptr[i] = i;
}
FILE *fp = fopen("F:\\Labwindows cvi\\test.txt","ab+");
fwrite(Ptr,sizeof(unsigned int),DATA_SIZE,fp);
fclose(fp);
free(dataPtr);
system("pause");
return 0;
}
fread()读数据:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *filp = NULL;
char fileDir[] = "/home/yangzhiyuan/Documents/test.txt";
char dataPtr[] = "Helloworld";
printf("sizeof(dataPtr) = %ld\n",sizeof(dataPtr));
filp = fopen(fileDir,"w+"); /* 可读可写,不存在则创建 */
int writeCnt = fwrite(dataPtr,sizeof(dataPtr),1,filp); /* 返回值为1 */
//int writeCnt = fwrite(dataPtr,1,sizeof(dataPtr),filp); /* 返回值为11 */
printf("writeCnt = %d\n",writeCnt);
fclose(filp);
FILE *fp = NULL;
fp = fopen(fileDir,"r");
char buffer[256];
int readCnt = fread(buffer,sizeof(buffer),1,fp); /* 返回值为0 */
//int readCnt = fread(buffer,1,sizeof(buffer),fp); /* 返回值为11 */
printf("readCnt = %d\n",readCnt);
fclose(fp);
printf("%s\n",buffer);
exit(0);
}
参考该链接:(https://blog.csdn.net/yang2011079080010/article/details/52528261)