fgets() not working after fscanf()
Asked Answered
R

3

2

I am using fscanf to read in the date and then fgets to read the note. However after the first iteration, fscanf returns a value of -1.

I used GDB to debug the program step by step. It works fine until the first use of fgets. When I try print out the line read by fgets on the first iteration, it gives me this:

(gdb) print line
$6 = "\rtest\r18/04/2010\rtest2\r03/05/2010\rtest3\r05/08/2009\rtest4\r\n\000\000\000\000q\352\261\a\370\366\377\267.N=\366\000\000\000\000\003\000\000\000\370xC\000\000\000\000\000\000\000\000\000\001\000\000\000\227\b\000\000\070\367\377\267H\364\377\267\362\202\004\bdoD\000\354\201\004\b\001\000\000\000\304oC\000p\363\377\277\260zC\000D\363\377\277\n!B\000\064\363\377\277\354\201\004\b(\363\377\277TzC\000\000\000\000\000\070\367\377\267\001\000\000\000\000\000\000\000\001\000\000\000\370xC\000\001\000\000\000\000\000\312\000\000\000\000\000\377\260\360\000\001\000\000\000\277\000\000\000\364\317\000\000\344\261\\\000\000\000\000\000p\363\377\277|\233\004\b\350\362\377\277 \204\004\b\005\000\000\000|\233\004\b\030\363\377\277"

It looks like fgets reads the remaining entries and then stores them all in a single string.

I am not sure why it is doing this.

Here is the main code:

int main(int argc, char* argv[]) {
    FILE* file;
    int numEntries, i = 0;
    int index = atoi(argv[1]);
    char line[SIZE];
    JournalEntry *entry;

    /*argument provided is the entry user wants to be displayed*/
    if (argc > 2) {
        perror("Error: Too many arguments provided");
    }
    file = fopen("journalentries.txt", "r");
    if (file == NULL) {
        perror("Error in opening file");
    }

    if (fscanf(file, "%d", &numEntries) != 1) {
        perror("Unable to read number of entries");
    }

    entry = (JournalEntry*)malloc(numEntries  * sizeof(JournalEntry));
    if (entry == NULL) {
        perror("Malloc failed");
    }

    for (i = 0; i < numEntries; i++) {
        if (fscanf(file, "%d/%d/%d", &entry[i].day, &entry[i].month, &entry[i].year) != 3) {
            perror("Unable to read date of entry");
        }

        if (fgets(line, sizeof(line), file) == NULL) {
            perror("Unable to read text of entry");
        }
    }

    printf("%d-%02d-%02d %s: ", entry[index].year, entry[index].month, entry[index].day, entry[index].text);

    if(ferror(file)) {
        perror("Error with file");
    }

    fclose(file);
    free(entry);

    return 0;
}

The file that I have to read: The very first line contains the number of entries to be read

4
12/04/2010
test
18/04/2010
test2
03/05/2010
test3
05/08/2009
test4

The struct JournalEntry located in the header file:

typedef struct {
    int day;
    int month;
    int year;
    char text[250];
} JournalEntry;
Refugia answered 14/10, 2013 at 15:39 Comment(5)
Where does the input file come from, using \r as line terminator? Line ends should be \n.Angelineangelique
I created the input file using notepad++Refugia
@Refugia try with numEntries -1Patricide
Do NOT cast the return value of malloc()!Ind
+1 for checking the results of fscanf().Vella
B
1

It looks like fgets reads the remaining entries and then stores them all in a single string.

Yes, '\r' is not line terminator. So when fscanf stops parsing at the first invalid character, and leaves them in the buffer, then fgets will read them until end of line. And since there are no valid line terminators in the file, that is until end of file.

You should probably fix the file to have valid (Unix?) line endings, for example with suitable text editor which can do it. But that is another question, which has been asked before (like here), and depends on details not included in your question.

Additionally, you need dual check for fscanf return value. Use perror only if return value is -1, otherwise error message will not be related to the error at all. If return value is >=0 but different from what you wanted, then print custom error message "invalid input syntax" or whatever (and possibly use fgets to read rest of the line out of the buffer).

Also, to reliably mix scanf and fgets, I you need to add space in the fscanf format string, so it will read up any whitespace at the end of the line (also at the start of next line and any empty lines, so be careful if that matters), like this:

int items_read = scanf("%d ", &intvalue);

As stated in another answer, it's probably best to read lines with fgets only, then parse them with sscanf line-by-line.

Baro answered 14/10, 2013 at 15:47 Comment(1)
I recreated the input file in gvim. Adding a space in the fscanf format string did the trick. Thanks!Refugia
L
1

Don't mix fscanf() and fgets(), since the former might leave stuff in the stream's buffer.

For a line-oriented format, read only full lines using fgets(), then use e.g. sscanf() to parse what you've read.

Lewandowski answered 14/10, 2013 at 15:47 Comment(0)
W
0

The string you see when running GDB really ends at the first null character:

"\rtest\r18/04/2010\rtest2\r03/05/2010\rtest3\r05/08/2009\rtest4\r\n\000"

The other data after is ignored (when using ordinary str-functions);

Whew answered 14/10, 2013 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.