
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Write a C program to read a data from file and display
Problem
How to read a series of items that are present in a file and display the data in columns or tabular form using C Programming
Solution
Create a file in write mode and write some series of information in the file and close it again open and display the series of data in columns on the console.
Write mode of opening the file
FILE *fp; fp =fopen ("sample.txt", "w");
If the file does not exist, then a new file will be created.
If the file exists, then old content gets erased & current content will be stored.
Read mode of opening the file
FILE *fp fp =fopen ("sample.txt", "r");
If the file does not exist, then fopen function returns NULL value.
If the file exists, then data is read from the file successfully.
The logic used to display data on the console in tabular form is −
while ((ch=getc(fp))!=EOF){ if(ch == ',') printf("\t\t"); else printf("%c",ch); }
Program
#include <stdio.h> #include<ctype.h> #include<stdlib.h> int main(){ char ch; FILE *fp; fp=fopen("std1.txt","w"); printf("enter the text.press cntrl Z:
"); while((ch = getchar())!=EOF){ putc(ch,fp); } fclose(fp); fp=fopen("std1.txt","r"); printf("text on the file:
"); while ((ch=getc(fp))!=EOF){ if(ch == ',') printf("\t\t"); else printf("%c",ch); } fclose(fp); return 0; }
Output
enter the text.press cntrl Z: Name,Item,Price Bhanu,1,23.4 Priya,2,45.6 ^Z text on the file: Name Item Price Bhanu 1 23.4 Priya 2 45.6
- Related Articles
- Write a Python program to read an Excel data from file and read all rows of first and last columns
- Read Data from a Text File using C++
- How to create a file, write data into it and read data from it on iOS?
- Read and write a string from a text file
- How to read data from PDF file and display on console in Java?
- How to read/write data from/to .properties file in Java?
- Write a program in Python to read CSV data from a file and print the total sum of last two rows
- Write a Python code to read JSON data from a file and convert it to dataframe, CSV files
- Read/Write structure to a file using C
- How to read data from a file using FileInputStream?
- Read/Write Class Objects from/to File in C++
- How to read the data from a file in Java?
- Write a program in Python to read sample data from an SQL Database
- How to read and write a file using Javascript?
- Write a program in Python to export a given dataframe into Pickle file format and read the content from the Pickle file

Advertisements