How to read text files and put words into an array?

Asked

Viewed 19,917 times

2

I need to make a code that reads a text file (txt) and save only the words at the positions of a vetor, below follows my code:

int main(int argc, char** argv) 
{
    FILE *file;
    file = fopen("arquivo.txt", "r");

    char x[100];

    while((x[i] = fgetc(file)) != ' ' && x[i] != '\t')
    {
        i++;
    }

    j = 1;

    x[i+1] = '\0';
    printf("%s", x);

    fclose(file);

    return (EXIT_SUCCESS);
}

But from that I don’t know how to do.

  • 2

    Have you started? What have you done so far?

  • I read the first word down to the first space, but then I don’t know how to continue reading and storing.

  • Post the code.

1 answer

4

You have to keep in mind that a string vector on C is actually a matrix of char, See the example below:

char* palavras[50];

Above I declared a vector with a pointer pointing to the word. This vector has the capacity to store 50 words, and the number of characters of the words can be any one, because it will depend on the size of the word that will be in the file palavras.txt.

I assumed that the content structure of the text file palavras.txt is as follows:

computador
gato
mundo
cachorro
casa
stack
over
flow

Here follows an example of a program that reads the words of the text file and stores in the vector palavras the words read:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int i = 0;
    int numPalavras = 0;
    char* palavras[50];
    char line[50];

    FILE *arquivo;
    arquivo = fopen("palavras.txt", "r");

    if (arquivo == NULL)
        return EXIT_FAILURE;

    while(fgets(line, sizeof line, arquivo) != NULL)
    {
        //Adiciona cada palavra no vetor
        palavras[i] = strdup(line);

        i++;

        //Conta a quantidade de palavras
        numPalavras++;
    }

    int j;

    for (j = 0; j < numPalavras; j++)
        printf("\n%s", palavras[j]); //Exibi as palavras que estao no vetor.

    printf("\n\n");

    fclose(arquivo);

    return EXIT_SUCCESS;
}

Exit from the program:

computador

gato

mundo

cachorro

casa

stack

over

flow

Notice that I had to use the function strdup to return the string pointer that is stored in line which in this case is the word, without it all the pointers of the words would point to the same location, if I’m not mistaken it would point to the last word of the file flow, to check just do the test by replacing the line palavras[i] = strdup(line); for palavras[i] = line; to see the result.

To learn more about the function strdup see this question.

Sources:
Reading Lines from c file and putting the strings into an array.
How do I create an array of strings in C?

  • This program does not do what it promises. It only puts the words of the text in a vector if each line has only one word.

  • @Claudius the AP did not specify this in the question, and I answered what was requested. This has no relation to how the file is organized because it was not even exemplified in the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.