0

Well, i'm trying to read from a file, this is my file reader:

int readFile(char strArray[MAX][MAX]) {
    FILE *infile;
    infile = fopen("input.txt", "r");
    int row = 0;
    if(infile == NULL)
        exit(0);
    while(feof(infile) == 0) {
        fgets(strArray[row], 100, infile);
        row++;
    }
    fclose(infile);
    return row;
}

and the "input.txt" file i was trying to read:

hello
goodbye
**an empty line***

the problem occurred when i left the last line as empty it will print out weird symbols, i have fixed it by removing the empty line but i am wondering if there is a way to leave the empty line but not print out weirds symbols, i have tried (C - Reading from a file issue with the last line) but it just freezed my compiler.

CosmosLee
  • 27
  • 5
  • 1
    Try changing your reading loop to this: https://prnt.sc/1306ih7 (if the links doesn't open, try again later.. sometimes Lightshot goes down). It removes `'\n'` from the end of the lines while parsing the text-file . – Harry K. May 16 '21 at 17:20
  • 1
    Not checking the return value of `fgets()` is a key mistake. Check it and drop using `feof()`. – chux - Reinstate Monica May 17 '21 at 03:47

0 Answers0