简体   繁体   中英

Unable to Read file in C

I am a beginner at C and am using DevC++. I have written the code below and wish to read in the data written in input.txt. However when I try to run the code, I always receive the "Cant open file" message. It seems unable to find input.txt, and I am entirely unsure how to change that.

int T;
char command;
FILE *inputfile;

inputfile = fopen("input.txt", "r");

if(inputfile == NULL)
{
    printf("Cant open file");
}

fscanf(inputfile, "%d", &T);

It can be because of the following reasons

  1. File you are reading and your source code are not in same directory
  2. You have misspelled the file name

Otherwise your code is correct there should be no problem

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