#include <stdio.h>
int main()
{
FILE *fp;
int text=987654321;//内存中(10001101 000101110 01111011 01011100)-(177 104 222 58)
int text1=0,text2=0,text3=0,text4=0;
if((fp=fopen(“stu_test“,“w+“))==NULL)
{
printf(“cannot open the file!\n“);
return 0;
}
fwrite(&text,sizeof(int),1,fp);
rewind(fp);
fseek(fp,0,0);
fread(&text1,1,1,fp);
printf(“%d\n“,text1);
fseek(fp,1,0);
fread(&text2,1,1,fp);
printf(“%d\n“,text2);
fseek(fp,2,0);
fread(&text3,1,1,fp);
printf(“%d\n“,text3);
fseek(fp,3,0);
fread(&text4,1,1,fp);
printf(“%d\n“,text4);
fclose(fp);
return 0;
}
输出结果是正确的,不过为什么要重新定义变量,用fread读给原来的text却不行呢?而需要重新定义text1,2,3…
>> 本文固定链接: http://www.vcgood.com/archives/3339
#include <stdio.h>
int main()
{
FILE *fp;
int text=987654321;//内存中(10001101 000101110 01111011 01011100)-(177 104 222 58)
int text1=0,text2=0,text3=0,text4=0;
if((fp=fopen(“stu_test“,“w+“))==NULL)
{
printf(“cannot open the file!\n“);
return 0;
}
fwrite(&text,sizeof(int),1,fp);
rewind(fp);
fseek(fp,0,0);
fread(&text1,1,1,fp);
printf(“%d\n“,text1);
fseek(fp,1,0);
fread(&text2,1,1,fp);
printf(“%d\n“,text2);
fseek(fp,2,0);
fread(&text3,1,1,fp);
printf(“%d\n“,text3);
fseek(fp,3,0);
fread(&text4,1,1,fp);
printf(“%d\n“,text4);
fclose(fp);
return 0;
}
输出结果是正确的,不过为什么要重新定义变量,用fread读给原来的text却不行呢?而需要重新定义text1,2,3…
void rewind(FILE * stream);
设一个n接受你fwrite 的返回值,然后读的时候直接就读n个字节、、、