Getting extra line from reading in a file with a while loop (scan("%s", &2dArray)) != EOF) in C, why is this?

61 Views Asked by At

I am trying to read in a file with 35 lines of text, the numbers in the file are being converted to letter grades and outputted to the console later in the code.

The file itself is a text file with 35 lines, the first line being skipped in this code and the rest being read in,

examples of the lines in the file look like (the bold is what I am trying to read)

Image of the file

601000002001010906001   5323 #0001 Y  S                AAAAAA AAA A        **1122334321135 113132311412413  **                                                                                                                                                                         009 
601000003001010906001   5323 #0001 Y  S                BBB BBBBBBB B       **12231342434321223333122142111 **                                                                                                                                                                          005 
601000004001010906001   5323 #0001 Y  S                CCCCCCCCC CCCCC     **24342323213151331233454432214   **                                                                                                                                                                        005 

Attached is an image with the output, the 35th line should not be there. Image of the current console output, line 35 should not be here

My code looks like this (yes, it may be all over the place, I have been trying to fix this problem for a while now through a ton of debugging.

int getAnswer(char answer[][TOTAL_QUESTIONS])
{
    char c;
    int row = 0;
    int column = 0;
    char garbage[1000];

    while (scanf("%s", &answer[row][column]) != EOF)
    {
        // if(row < 34) {
        skipNchar(77);
        for (column = 0; column < TOTAL_QUESTIONS; column++)
        {
                scanf("%c",&c);
                if (c == ' ' || c == '*')
                    answer[row][column]=' ';
                else
                    answer[row][column] = c;
        }
        // scanf("%c", &c);
        answer[row][column] = '\0';
        row++;
        column = 0; //}
    }

    return row;
}

Why do I continue to read in an extra line of garbage from the file?

0

There are 0 best solutions below