How to use the fscanf function to read file word by word in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

int main() {
    const char* filename = "d:\\data.txt";

    FILE *fp = fopen(filename, "r");

    struct stat sb;
    stat(filename, &sb);

    char *file_content = malloc(sb.st_size);

    while (fscanf(fp, "%[^\n ] ", file_content) != EOF) {
        printf("%s\n", file_content);
    }

    fclose(fp);
    free(file_content);

    return 0;
}





/*
run

c
c++
C#
NODEjs
eXPRESS
jAVAsCRIPT
php

*/

 



answered May 2, 2021 by avibootz

Related questions

1 answer 172 views
1 answer 139 views
2 answers 279 views
2 answers 188 views
1 answer 185 views
185 views asked Jun 15, 2018 by avibootz
...