Q:

C - Read Content of a File using getc() using C Program

0

C - Read Content of a File using getc() using C Program

In this program (code snippet) you will learn how to read content of a file using getc() function in c programming language file handling?

In this example assume there is a file named "sample.txt" which contains text "This is sample.txt file document". By using c program file handling we will print characters on the output screen using getc() function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C Code: Read Content of a File using getc() using C Program

/*C - Read Content of a File using getc() 
using C Program.*/
 
#include <stdio.h>
 
int main(){
 
    //file nane
    const char *fileName="sample.txt";
    //file pointer
    FILE *fp;
    //to store read character
    char ch;
     
    //open file in read mode
    fp=fopen(fileName,"r");
    if(fp==NULL){
        printf("Error in opening file.\n");
        return -1;
    }
    printf("Content of file\n");
    while((ch=getc(fp))!=EOF){
        printf("%c",ch);
    }
    fclose(fp);
     
    return 0;
}

Output

    Content of file 
    This is sample.txt file document.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

File Handling Examples Programs in C language

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C - Convert All Characters in Upper Case of a File... >>
<< C program to copy number of bytes from specific of...