why it prints out jibberish word(junk words)

Write your question here.
i want to put the words from a file to a dynamic array but when i printed out it prints exactly 4 words but are 4 junk words
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  #include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define WIDTH 50

int main()
{
    /**/
    char  **wordarr, *wordpc,ch ;
    int option=1, library, howmany, i,number, numwords=0,count=0;


    //open the file
    FILE *fp;
    fp = fopen("file.txt", "r");
    if (fp == NULL) {
        fprintf(stderr, "File not found\n");
        exit(1);
    }
    //counts the words in the file
    while((ch = fgetc(fp)) != EOF){
        if(ch == ' ' || ch == '\n' || ch == ',' || ch == '.'){
            count++;
        }
    }
    //making the array
    wordarr = (char**)malloc(sizeof(char*) * count);
    for(i = 0; i<count; i++){
        wordarr[i] = (char*)malloc(sizeof(char) * WIDTH);
    }
    //
    //putting the words into the array
    char buffer[10000];
    fgets(buffer, 10000, fp);

    char *tok;
    i=0;
    tok = strtok(buffer, " .,!");
    while (tok != NULL) {
        /*if (i >= howmany) {
            howmany *= 2;
            wordarr = (char**)realloc(wordarr, sizeof(char*) * howmany);
            for (i=howmany/2; i<howmany; i++) {
               wordarr[i] = (char*)malloc(sizeof(char) * WIDTH);
            }
            printf("realloc\n");
        }*/
        strcpy(wordarr[(i)++], tok);
        tok = strtok(NULL, " .,!");
    }

    fclose(fp);

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


    //
    for (i=0; i<count; i++) {
        free(wordarr[i]);
    }
    free(wordarr);
}
Hello Elsino, i have found two problems with your code as it is.


1. allocate enough memory to accommodate all word

the condition on line 29 should be i<=count; or
you could initialize count to count =1 ; and leave the rest in place



2. after reading a file stream you should reinitialize it/reset it if you intend to use it later

the loop on line 22 reads from your fp till eof
trying to use fp on line 35 will only read eof

you should reset fp to the beginning of the file before calling fgets



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define WIDTH 50

int main()
{
    /**/
    char  **wordarr,ch ;
    int   i,count=1;//changed count from 0 to 1


    //open the file
    FILE *fp;
    fp = fopen("file.txt", "r");
    if (fp == NULL) {
        fprintf(stderr, "File not found\n");
        exit(1);
    }
    //counts the words in the file
    while((ch = fgetc(fp)) != EOF){
        if(ch == ' ' || ch == '\n' || ch == ',' || ch == '.'){
            count++;
        }
    }
    //making the array
    wordarr = (char**)malloc(sizeof(char*) * count);
    for(i = 0; i<count; i++){
        wordarr[i] = (char*)malloc(sizeof(char) * WIDTH);
    }
    //
    //putting the words into the array

    fclose(fp); ///reset fp to beginning
    fp = fopen("file.txt", "r");

    char buffer[10000];
    fgets(buffer, 10000, fp);
    char *tok;
    i=0;
    tok = strtok(buffer, " .,!");
    while (tok != NULL) {
        strcpy(wordarr[(i)++], tok);
        tok = strtok(NULL, " .,!");
    }

    fclose(fp);

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


    //
    for (i=0; i<count; i++) {
        free(wordarr[i]);
    }
    free(wordarr);
}


It's a result of a bug. You better detect and solve it as quick as you can. There are programs that might help you, like checkmarx, if you need .
Good luck.
Topic archived. No new replies allowed.