读书人

帮忙看一下这段文件读取的代码解决思路

发布时间: 2012-04-26 14:01:31 作者: rapoo

帮忙看一下这段文件读取的代码
#include<stdio.h>
#include <sys/stat.h>

main()
{
? struct stat buf;
? char a[12323];
? char c;
? char name[20];
? printf("Do you want to read a file?\n");
? c=getchar();
? if(c=='y'||c=='Y'){
printf("please enter the file name you want to read: ");
scanf("%s",name);
? freopen(name,"r",stdin);
? while(scanf("%s",a)!=EOF){
? printf(" %s ",a);
? }
? if(stat(name,&buf)==0){
? printf("%d",buf.st_atime);
? }
? printf("the file %s has been readed",name);
? freopen(name,"r",stdin);
? freopen("fd.doc","w+",stdout);
? while(scanf("%s",a)!=EOF){
? printf(" %s ",a);
? }
? }
}
虽然能读取纯文本文件,但是读其他文件时读不了,因为其它文件包含文件头,其中包括它的格式之类的信息,我想全都读进来,该怎么办?

[解决办法]
用二进制模式打开
[解决办法]
奇怪,你换2进制模式试下吧。
我试过直接读的时候,可以读出文件头的。

读书人网 >C语言

热点推荐