开发者

Not able to read file using C

Here is the code I have:

 #include<stdio.h>
 #include<stdlib.h>
void main()
{
 int n,i,a[40];
 char file_name[100];
 FILE *file;
 printf("enter the file name\n");
     scanf("%s",&file_name);
 printf("enter the size\n");
 scanf("%d",&am开发者_JS百科p;n);

 if((file=fopen(file_name,"rb"))==NULL)
 {
  printf("cant open a file\n");
  return;
 }
 fread(a,sizeof(int),n,file);
 for(i=0;i<n;i++)
 {
  printf("%s",a[i]);
 }
}


Change:

 scanf("%s",&file_name);

to:

 scanf("%s", file_name);

You also need to change:

 printf("%s",a[i]);

to:

 printf("%d", a[i]);

(and while you're at it, change void main to int main).


scanf("%s",&file_name);

you don't have to pass a pointer to file_name since it's already a pointer. Change this line to:

scanf("%s", file_name);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜