user9452443
user9452443

Reputation:

C adding array a word

#include <stdio.h>

int main()
{
    char in_name[80];
    FILE *in_file;
    char word[50];
    char word2[50];
    char word3[50];
    char *strs[]= {"foo ABAA", "bar", "bletch","4"};
    strcpy(*strs[4],"wow");
    in_file = fopen("test2.txt", "r");

    if (in_file == NULL)
        printf("Can't open %s for reading.\n", in_file);
    else
    {
        while (fscanf(in_file, "%s %s %s ", word,word2,word3) != EOF)
        {

            printf("%s ", word);
            printf("%s ", word2);
            printf("%s\n", word3);
        }
        fclose(in_file);
    }
    printf("%s",strs[3]);
    int result = atoi(strs[3] );
    printf("\n");
    printf("%d",result);
    return 0;
}

Im new in C.Im trying to add the "woo" string to the strs array.But it gives an error.Im open to any help.My main point is reading line by line a txt file and adding every word to a array like {"what", "a", "nice","day"}

Upvotes: 0

Views: 54

Answers (1)

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

There are a few steps you must take:

To read a potentially unlimited number of words from your file, you must have an array strs that can hold enough words. Let's say 50:

char *strs[50]= 0;

But this is an array of pointers to strings; the pointers still point nowhere. So you must have memory to store the strings read. We will allocate the memory after each word read.

You open the file and start reading. For each word read, you allocate the memory and store it in the strs array. Then you copy the word to the memory you have just allocated:

    int i= 0;
    while (fscanf(in_file, "%s %s %s ", word,word2,word3) != EOF)
    {
        strs[i]= malloc(strlen(word )+1; strcpy(strs[i],word ); i++;
        strs[i]= malloc(strlen(word2)+1; strcpy(strs[i],word2); i++;
        strs[i]= malloc(strlen(word3)+1; strcpy(strs[i],word3); i++;
    }
    fclose(in_file);

Thats's all...


what you did wrong and needs noticing:

char *strs[]= {"foo ABAA", "bar", "bletch","4"};

Ths is an array that you initialize with the values between { and }. You do not specify the size and the size is taken by counting the number of initializers between the { and }.

You initialize the array with literal strings. These are constants and cannot be modified. So when you do:

strcpy(*strs[4],"wow");

you do 4 things wrong: 1) entry 4 is beyond your array; 2) you dereference a pointer (strs[4] is already a pointer, so no * needed), and even if we forget this last error, you try 3) to overwrite a read-only string, and that is not allowed. And finally, 4) the string you try to copy is much longer than the string that was there, so even if the string was not read-only, you make an error.

while (fscanf(in_file, "%s %s %s ", word,word2,word3) != EOF)

This use of scanf is not really wrong but it is always better to compare the result with the number of items you want to read. 3 here. Also, we still must guard against reading too many words:

while (i<47 && fscanf(in_file, "%s %s %s ", word,word2,word3)==3)

Upvotes: 2

Related Questions