C Program To Read A Text File

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

  1. C Program to Convert Decimal to Hexadecimal
  2. C Program to Convert Decimal to Binary
  3. C Program to Convert Celsius to Fahrenheit
  4. C Program To Find Absolute Value
  5. Ternary Operator Program in C
  6. C Program For Addition Table Using For Loop
  7. Denomination Program in C
  8. C Program to Print Multiplication Table 
  9. C Program Array with Example
  10. Binary Search in C Program Using Recursion
  11. Bubble Sort in C Using Pointers
  12. Bubble Sort Program in C Using Recursion
  13. Bubble Sort Program in C Using Array
  14. Bubble Sort Program in C Using Function
  15. Bubble Sort Program in C Using Linked List
  16. Stack Push Pop Program in C Using Arrays
  17. Factorial Program in C Using Pointers
  18. Factorial Program in C Using While Loop
  19. C Program For Factorial Using For Loop
  20. Factorial Program in C Using Recursion Function

Leave a Comment