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 print all the words of a text file with C

2 Answers

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

void print_words(char filename[]) {
    FILE* fp = fopen(filename, "r");

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

    char word[16];
    while (fscanf(fp, "%s", word) != EOF) {
        puts(word);
    }

    fclose(fp);
}

int main()
{
    print_words("d:\\data.txt");

    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 Jul 9, 2020 by avibootz
edited Jun 19, 2024 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

void print_words(char *filename) {

    FILE* fp;
    char buffer[255];

    fp = fopen(filename, "r");
    if (!fp) return 1;

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

        p = strtok(buffer, " ,.-\n");
        while (p != NULL) {
            printf("%s\n", p);
            p = strtok(NULL, " ,.-\n");
        }
    }

    fclose(fp);
}

int main(void)
{
    print_words("d:\\data.txt");

    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

Related questions

1 answer 153 views
1 answer 162 views
2 answers 118 views
1 answer 165 views
1 answer 161 views
1 answer 225 views
225 views asked Apr 8, 2015 by avibootz
1 answer 199 views
...