deejai
deejai

Reputation: 27

C fscanf unexpectedly pulling content from next line

EDIT: OK I found the embarrassing mistake. My description array wasn't big enough for that line...

I'm trying to extract related abbreviations and descriptions from a simply formatted text file, but I'm running into a problem on just a single line while every other line works fine.

In the text file I'm reading from, lines 5-7 are:

5. FW Foreign word
6. IN Preposition or subordinating conjunction
7. JJ Adjective

What I'm trying to do with each line is read the abbreviation and store it as a character array and then do the same with the description. For every line except for #6, it works fine.

What I expect is:

print decription[line6]   => "Preposition or subordinating conjunction"

But what I get is:

print decription[line6]   => "Preposition or subordinating conjunctio"Adjective"

I'm pretty lost as to why it might be doing that. It seems to be reading data from the next line. Or maybe I ended up overwriting the next line into that array.

#include <stdio.h>

int main(){
    FILE *fileToRead = fopen("PennTreebank_POS_Tags.txt", "r");
    FILE *fileToWrite = fopen("newFile.txt", "w");

    int i, j;
    i = j = 0;
    int nextChar;

    char abbreviation[50][5];
    char description[50][40];

    while( fscanf(fileToRead, "%*s %s ", abbreviation[i]) != EOF ){

        description[i][0] = '"';
        while( ((nextChar = fgetc(fileToRead)) != '\n') && (nextChar != EOF) ){
            description[i][j] = nextChar;
            j++;
        }
        description[i][j] = '"';
        description[i][j+1] = '\0';

        j=1;
        i++;
    }

    for( i=0; i<36; i++ ){
        printf("%s %s\n", abbreviation[i], description[i]);
    }
}

Upvotes: 2

Views: 45

Answers (1)

Weather Vane
Weather Vane

Reputation: 34585

The text "Preposition or subordinating conjunction" has a length of 40 chars.

So the array

char description[50][40];

will not be enough size to hold the length 40 plus the 0 terminator.

Upvotes: 3

Related Questions