|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
程序实现写入三个数据到文件,并读取数据显示到终端。
程序部分:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define NUM 3
struct test
{
char name[20];
int size;
}wr[NUM];
struct test re[NUM],second;
void set_test(struct test *pts, const char *ptr)
{
strcpy(pts->name, ptr);
pts->size = strlen(ptr);
}
int main(void)
{
FILE *fp;
int i;
set_test(&wr[0], "Linux");
set_test(&wr[1], "FreeBSD");
set_test(&wr[2], "windows");
fp = fopen("test.dat", "w");
fwrite(wr, sizeof(struct test), NUM, fp);
fclose(fp);
fp = fopen("test.dat", "r");
fread(re, sizeof(struct test), NUM, fp);
fseek(fp,-2*sizeof(struct test),SEEK_END);
fread(&second,sizeof(struct test),1,fp);
fclose(fp);
for(i = 0; i < NUM; i++)
{
printf("name[%d]=%-20s, size[%d]=%d\n",
i, re[i].name, i, re[i].size);
}
printf("Second name=%-20s, size=%d\n",second.name,second.size);
return 0;
}
红字部分何解,,新手求助;
|
|