简体   繁体   中英

How can I read integer from file with cmd in c

This is input.txt

5343
232
5
    
int main(int argc, char *argv[]) {
    int x;
    int sum=0;
    
    int count=0;
       if(argc==1)
        printf("Error message!");
    if(argc>=2)
    {

    FILE* file = fopen (argv[1], "r");
    
  while(!feof(file)){
    fscanf(file,"%d",&x);
    sum+=x;
        count++;
}
    printf("%d", sum);
    printf("%d", count);
  return 0;
}

I use this main and write this in cmd= c_file.exe input.txt

After that statement in cmd, there is no output and I can't write anything in cmd.

input.txt is given above I want to read and store the integer digit by digit how can I do it?

  while(fscanf(file,"%d",&x) == 1)
  { 
    sum+=x;
    count++;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM