0

So I tried to get the codewords from a file and put them into my words array. When I print them inside the for loop I get all the codewords from the .txt file but when I try to print one of the elements of my array, it always gives the last line from the text file. How can I fix this?

This is my text file's content: ape car big 123 book door epic band test apple catch super level small basket abroad action carbon program buzzcut jimjams muzzles puzzles dazzled football baseball absolute campaign casualty division quizzical twizzling bedazzled fuzziness maximizer strawberry friendship motivation everything ==> each word in an another line.

char *words[0][39];

int main() {
    FILE *codewordsPtr;

    if((codewordsPtr = fopen("codewords.txt","rb")) == NULL){
        printf("file could not be opened");
    }

    char bum[12];
    int wordList = 0;

    for(int i=0; i<20; i+=5,wordList++){
        fseek(codewordsPtr, i, SEEK_SET);
        fgets(bum, 12, codewordsPtr);
        words[0][wordList] = bum;
        printf("%s" ,words[0][wordList]);
    }

    for(int i=20; i<50; i+=6,wordList++){
        fseek(codewordsPtr, i, SEEK_SET);
        fgets(bum, 12, codewordsPtr);
        words[0][wordList] = bum;
        printf("%s" ,words[0][wordList]);
    }

    for(int i=50; i<85; i+=7,wordList++){
        fseek(codewordsPtr, i, SEEK_SET);
        fgets(bum, 12, codewordsPtr);
        words[0][wordList] = bum;
        printf("%s" ,words[0][wordList]);
    }

    for(int i=85; i<117; i+=8,wordList++){
        fseek(codewordsPtr, i, SEEK_SET);
        fgets(bum, 12, codewordsPtr);
        words[0][wordList] = bum;
        printf("%s" ,words[0][wordList]);
    }

    for(int i=117; i<171; i+=9,wordList++){
        fseek(codewordsPtr, i, SEEK_SET);
        fgets(bum, 12, codewordsPtr);
        words[0][wordList] = bum;
        printf("%s" ,words[0][wordList]);
    }

    for(int i=171; i<231; i+=10,wordList++){
        fseek(codewordsPtr, i, SEEK_SET);
        fgets(bum, 12, codewordsPtr);
        words[0][wordList] = bum;
        printf("%s" ,words[0][wordList]);
    }

    for(int i=231; i<286; i+=11,wordList++){
        fseek(codewordsPtr, i, SEEK_SET);
        fgets(bum, 12, codewordsPtr);
        words[0][wordList] = bum;
        printf("%s" ,words[0][wordList]);
    }

    for(int i=286; i<334; i+=12,wordList++){
        fseek(codewordsPtr, i, SEEK_SET);
        fgets(bum, 12, codewordsPtr);
        words[0][wordList] = bum;
        printf("%s" ,words[0][wordList]);
    }

    printf("\n\n\n");
    printf("%s", words[0][5]);

    fclose(codewordsPtr);
    return 0;
}

This is my output: ape car big 123 book door epic band test apple catch super level small basket abroad action carbon program buzzcut jimjams muzzles puzzles dazzled football baseball absolute campaign casualty division quizzical twizzling bedazzled fuzziness maximizer everything

everything Process returned 0 (0x0) execution time : 0.015 s Press any key to continue.

  • 2
    `char *words[0][39];` 0 sized arrays are not allowed in standard C. – David Ranieri Jan 05 '22 at 13:03
  • 1
    @Berk Ustuner Also you are setting elements of the two-dimensional array to the address of the same array bum words[0][wordList] = bum; – Vlad from Moscow Jan 05 '22 at 13:13
  • @VladfromMoscow So how can I store the words in my text file in an array? – Berk Ustuner Jan 05 '22 at 13:20
  • Could you show your file's contents? Those magic number loops look needlessly rigid, and whatever you are trying to do could likely be solved more simply with better file formatting. – Oka Jan 05 '22 at 13:21
  • @BerkUstuner You need to allocate memory for each word. – Vlad from Moscow Jan 05 '22 at 13:22
  • @VladfromMoscow Thanks, I guess I need memory allocation. I thougth I could escape using it :) – Berk Ustuner Jan 05 '22 at 13:30
  • Use `strcpy()` of one of it's friends to copy strings. Make sure there is space allocated. Maybe you should be using `strdup()`. – Jonathan Leffler Jan 05 '22 at 13:32
  • @BerkUstuner Your file has consistent formatting - there is no need for the complicated loops and manual seeking through the file! Something as simple as `char a_word[64]; while (1 == fscanf(file, "%63s", a_word)) { /* do something with the buffer */ }` will get you each word in that file. It's up to you to allocate space (dynamically or statically), and **copy** the *contents* of the buffer each iteration. – Oka Jan 05 '22 at 13:44

0 Answers0