C

How to Read a File in C

In this tutorial, we are going to see how to read a file in C and display its content on the screen. fopen function is used to open a file. It returns a pointer to the FILE structure which is a predefined structure in the header file stdio.h. If the file is opened successfully, the fopen function returns a pointer to the file and if it fails to open the file, it returns NULL. The fgetc function returns the character read from the file and the fclose function closes the file. The file to be read must be present in the directory where the executable file of this program is located.
100-multiple-choice-questions-in-c-programming100 Multiple Choice Questions In C Programming – Part 1This collection of 100 Multiple Choice Questions and Answers (MCQs) In C Programming : Quizzes & Practice Tests with Answer focuses on “C Programming”.  …Read More

How to Read a File in C
#include <stdio.h>
#include <stdlib.h>

int main() {

    FILE *f;
    char c;
    f=fopen("test.txt","rt");

    while((c=fgetc(f))!=EOF){
        printf("%c",c);
    }

    fclose(f);
    return 0;
}

Output:
 

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *