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,885 questions

51,811 answers

573 users

How to read an entire file in C

1 Answer

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

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

    FILE* fp = fopen(filename, "r");
    if (!fp)
        exit(EXIT_FAILURE);

    struct stat sb;
    if (stat(filename, &sb) == -1) {
        perror("stat");
        exit(EXIT_FAILURE);
    }

    char* file_contents = malloc(sb.st_size);
    fread(file_contents, sb.st_size, 1, fp);

    printf("%s\n", file_contents);

    fclose(fp);
    free(file_contents);

    return 0;
}





/*
run

c c++ C#
NODEjs eXPRESS
jAVAsCRIPT php

*/

 



answered May 2, 2021 by avibootz

Related questions

1 answer 471 views
1 answer 223 views
1 answer 258 views
1 answer 264 views
1 answer 216 views
5 answers 415 views
2 answers 224 views
...