如题所述
æå¯ä»¥ç»ä½ ä¸äºCæä½æ件ç建议
åå ¥æ件使ç¨fwrite()å½æ°ï¼
æ¯è¾æ件ä¸å个æ°æ®çæå°å¼ï¼ä½ å¯ä»¥é个读åºï¼å¨æ¯è¾
ä¸é¢æ¯ä¸ä¸ªä¾å
#include<stdio.h>
int main()
{
FILE *fp;
int num[5]={10,20,50,40,60};
int i;
int number;
fp=fopen("Data","w"); //æå¼Dataæ件ï¼æ件æä½ä¸ºåå ¥
if(NULL==fp) //è¥æå¼å¤±è´¥ï¼è¿åNULL
{
printf("Can't open the file\n");
return;
}
for(i=0;i<5;i++) //å°numä¸çæ°æ®é个åå ¥
{
fwrite(&num[i],sizeof(int),1,fp);
}
fclose(fp); //å ³éæ件
fp=fopen("Data","r"); //éæ°æå¼ï¼æ件æä½ä¸ºè¯»
while(fread(&number,sizeof(int),1,fp)!=0) //é个读åæ件ä¸çæ°æ®ï¼å¹¶èµäºnumber
printf("\n%d",number); //è¾åºæ¯ä¸ªæ°æ®
fclose(fp);
}
åå ¥æ件使ç¨fwrite()å½æ°ï¼
æ¯è¾æ件ä¸å个æ°æ®çæå°å¼ï¼ä½ å¯ä»¥é个读åºï¼å¨æ¯è¾
ä¸é¢æ¯ä¸ä¸ªä¾å
#include<stdio.h>
int main()
{
FILE *fp;
int num[5]={10,20,50,40,60};
int i;
int number;
fp=fopen("Data","w"); //æå¼Dataæ件ï¼æ件æä½ä¸ºåå ¥
if(NULL==fp) //è¥æå¼å¤±è´¥ï¼è¿åNULL
{
printf("Can't open the file\n");
return;
}
for(i=0;i<5;i++) //å°numä¸çæ°æ®é个åå ¥
{
fwrite(&num[i],sizeof(int),1,fp);
}
fclose(fp); //å ³éæ件
fp=fopen("Data","r"); //éæ°æå¼ï¼æ件æä½ä¸ºè¯»
while(fread(&number,sizeof(int),1,fp)!=0) //é个读åæ件ä¸çæ°æ®ï¼å¹¶èµäºnumber
printf("\n%d",number); //è¾åºæ¯ä¸ªæ°æ®
fclose(fp);
}
温馨提示:答案为网友推荐,仅供参考