Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

How to insert all the words of a text file into an array in C

2 Answers

0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char** extract_words(char filename[], int size, int* total_words) {
    FILE* fp = fopen(filename, "r");

    if (fp == NULL) {
        printf("Error open file\n");
        exit(EXIT_FAILURE);
    }

    char word[32];
    char** array = malloc(sizeof(char*) * size);

    while (fscanf(fp, "%s", word) != EOF) {
        array[*total_words] = _strdup(word);
        (*total_words)++;
    }

    fclose(fp);

    return array;
}


int main()
{
    char filename[32] = "d:\\data.txt";
    int size = 1024, total_words = 0;

    char** array = extract_words(filename, size, &total_words);

    for (int i = 0; i < total_words; i++) {
        printf("%s\n", array[i]);
        free(array[i]);
    }

    free(array);

    return 0;
}




/*
run:

C
is
a
general-purpose
programming
language.
It
was
created
in
the
1970s
by
Dennis
Ritchie
and
remains
very
widely
used
and
influential.

*/

 



answered Jun 19, 2024 by avibootz
edited Jun 19, 2024 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char** extract_words(char filename[], int size, int *total_words) {
    FILE* fp = fopen(filename, "r");

    if (fp == NULL) {
        printf("Error open file\n");
        exit(EXIT_FAILURE);
    }

    char buffer[255];
    char word[32];
    char **array = malloc(sizeof(char *) * size);

    while (fgets(buffer, 255, fp) != NULL) {
        char *p;

        p = strtok(buffer, " ,.-\n");
        while (p != NULL) {
            array[*total_words] = _strdup(p);
            (*total_words)++;
            p = strtok(NULL, " ,.-\n");
        }
    }

    fclose(fp);

    return array;
}


int main()
{
    char filename[32] = "d:\\data.txt";
    int size = 1024, total_words = 0;

    char **array = extract_words(filename, size, &total_words);

    for (int i = 0; i < total_words; i++) {
        printf("%s\n", array[i]);
        free(array[i]);
    }

    free(array);

    return 0;
}




/*
run:

C
is
a
general
purpose
programming
language
It
was
created
in
the
1970s
by
Dennis
Ritchie
and
remains
very
widely
used
and
influential

*/

 



answered Jun 19, 2024 by avibootz
edited Jun 19, 2024 by avibootz

Related questions

1 answer 153 views
2 answers 169 views
1 answer 162 views
1 answer 159 views
1 answer 171 views
1 answer 143 views
1 answer 153 views
...