C Program To Read A Text File. If you are looking for a C program to read a text file and display example, this C programming tutorial will help you to learn how to write a program for reading a text file in C. Just go through this C programming example to learn about reading a file program in C.
C Program To Read A Text File
Learn how to write a c Program to read a text file. Writing IO file reading program in C can be done using various techniques but here in this program, we show how to write a c program to read a line from file and display in a proper way. Happy coding.
C Program to Read Text from a File – Source Code
You can copy paste the below C Program to read text from a file Using while fgetc() function, in c compiler to check how the source code work.
Example Source Code:
/* C program to read text from a file - ReadingFile.C */ #include <stdio.h> #include <stdlib.h> //for exit() function void main() { //variables declaration FILE *fptr; char filename[15]; char ch; //asking user to enter file name printf("Enter the filename to be opened \n"); //rading file name from user scanf("%s", filename); /* opening the file for reading */ fptr = fopen(filename, "r"); if (fptr == NULL) { printf("Cannot open file \n"); exit(0); } ch = fgetc(fptr); while (ch != EOF) { printf ("%c", ch); ch = fgetc(fptr); } fclose(fptr); }
C Program to Read Text from a File – Output
After you compile and run the above c program to read text from a file and display, your C compiler asks you to enter the filename to read the text and display. After you enter a .txt file name, the compiler will read the file and display its text.
C PROGRAMMING EXAMPLES
- C Program to Convert Decimal to Hexadecimal
- C Program to Convert Decimal to Binary
- C Program to Convert Celsius to Fahrenheit
- C Program To Find Absolute Value
- Ternary Operator Program in C
- C Program For Addition Table Using For Loop
- Denomination Program in C
- C Program to Print Multiplication Table
- C Program Array with Example
- Binary Search in C Program Using Recursion
- Bubble Sort in C Using Pointers
- Bubble Sort Program in C Using Recursion
- Bubble Sort Program in C Using Array
- Bubble Sort Program in C Using Function
- Bubble Sort Program in C Using Linked List
- Stack Push Pop Program in C Using Arrays
- Factorial Program in C Using Pointers
- Factorial Program in C Using While Loop
- C Program For Factorial Using For Loop
- Factorial Program in C Using Recursion Function