I don't know what's wrong but the succeeding strings are not properly read by fgets.

else {
    initArray(word1); //first string
    fgets(word1, 29, fileIn);
    printf("word1: %s\n", word1);
    len = strlen(word1);
    if(word1[len-1] == '\n')
    word1[len-1] = '\0';

    while(!feof(fileIn)) {     //succeeding strings in next line
        initArray(word1);
        fgets(word1, 30, fileIn);
        printf("word1: %s\n", word1);

        len = strlen(word1);
        if(word1[len-1] == '\n')
            word1[strlen(word1)-1] = '\0';
            fgetc(fileIn);
        } 
            fclose(fileIn);
    }
}

texts:
ABCD
asnjf
fkj

read:
ABCD
snjf
kj

Can you give me an idea what's wrong?

what is inarray()? You have too much duplicate code in that function. This is the recommended way to write such functions, not that it does not call feof() at all, but relies on fgets to return NULL then end-of-file is reached.

else {

    while(fgets(word1, 30, fileIn) != NULL) {     //succeeding strings in next line
        initArray(word1);
        fgets(word1, 30, fileIn);
        printf("word1: %s\n", word1);

        len = strlen(word1);
        if(word1[len-1] == '\n')
            word1[strlen(word1)-1] = '\0';
    }
    fclose(fileIn);

}

in the method initArray(char arr[]), each char is initialized to '\0'.... when I use fgets, does it change fully the value of the first value of word1[]?

I noticed that the fgetc that causes the first char of the next string to be not read, so I think that solves it

You can just remove initArray() because it's not necessary to initialize the string before calling fgets(). And there's a much easier and quicker way to do it too -- memset(word1,0,sizeof(word1));

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.