#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…
-
近期文章
近期评论
- coolker 发表在《打造最快的Hash表》
- struggle 发表在《提供C语言教学课件(适用于初学者)》
- zhanghaibo 发表在《提供C语言教学课件(适用于初学者)》
- zhanghaibo 发表在《提供C语言教学课件(适用于初学者)》
- diys 发表在《C语言编程宝典(王大刚) 1.1 C 语言的产生与发展》
文章归档
- 2022 年十月
- 2014 年一月
- 2013 年十二月
- 2012 年十一月
- 2012 年七月
- 2012 年六月
- 2012 年五月
- 2012 年四月
- 2012 年三月
- 2012 年二月
- 2011 年十二月
- 2011 年十月
- 2011 年九月
- 2011 年八月
- 2011 年七月
- 2011 年六月
- 2011 年五月
- 2011 年四月
- 2011 年三月
- 2011 年二月
- 2011 年一月
- 2010 年十二月
- 2010 年十一月
- 2010 年十月
- 2010 年九月
- 2010 年八月
- 2010 年七月
- 2010 年六月
- 2010 年五月
- 2010 年四月
- 2010 年三月
- 2010 年二月
- 2010 年一月
- 2009 年十二月
- 2009 年十一月
- 2009 年十月
- 2009 年九月
- 2009 年八月
- 2009 年七月
- 2009 年六月
- 2009 年五月
- 2009 年四月
- 2009 年三月
- 2009 年二月
- 2009 年一月
- 2008 年十二月
- 2008 年十一月
- 2008 年十月
- 2008 年九月
- 2008 年八月
- 2008 年七月
- 2008 年六月
- 2008 年五月
- 2008 年四月
- 2008 年三月
- 2008 年二月
- 2008 年一月
- 2007 年十二月
- 2007 年十一月
- 2007 年十月
- 2007 年九月
- 2007 年八月
- 2007 年七月
- 2007 年六月
- 2007 年三月
- 2007 年二月
- 2007 年一月
- 2006 年十二月
- 2006 年十一月
- 2006 年十月
- 2006 年九月
- 2006 年八月
- 2006 年七月
- 2006 年六月
- 2006 年五月
- 2006 年四月
- 2006 年三月
- 2006 年二月
- 2006 年一月
- 2005 年十二月
- 2005 年十一月
分类目录
功能
#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个字节、、、