
- 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
How to display the complete text one word per line in C language?
First, open the file in write mode. Later on, enter the text until it reaches the End Of File (EOF) i.e. press ctrlZ to close the file.
Again, open in reading mode. Then, read words from the file and print each word in a separate line and close the file.
The logic we implement to print one word per line is as follows −
while ((ch=getc(fp))!=EOF){ if(fp){ char word[100]; while(fscanf(fp,"%s",word)!=EOF) // read words from file{ printf("%s
", word); // print each word on separate lines. } fclose(fp); // close file. } }
Example
Following is the C program to display the complete text one word per line −
#include<stdio.h> int main(){ char ch; FILE *fp; fp=fopen("file.txt","w"); //open the file in write mode printf("enter the text then press cntrl Z:
"); while((ch = getchar())!=EOF){ putc(ch,fp); } fclose(fp); fp=fopen("file.txt","r"); printf("text on the file:
"); while ((ch=getc(fp))!=EOF){ if(fp){ char word[100]; while(fscanf(fp,"%s",word)!=EOF) // read words from file{ printf("%s
", word); // print each word on separate lines. } fclose(fp); // close file. } Else{ printf("file doesnot exist"); // then tells the user that the file does not exist. } } return 0; }
Output
When the above program is executed, it produces the following result −
enter the text then press ctrl Z: Hi Hello Welcome To My World ^Z text on the file: Hi Hello Welcome To My World
- Related Articles
- How to read complete text file line by line using Python?
- How to display all the MySQL tables in one line?
- How to list one filename per output line in Linux?
- How to word-wrap text in Tkinter Text?
- How to display mean line per group in facetted graph using ggplot2 in R?
- How to Find Line Number of a Given Word in text file using Python?
- How to display multiple labels in one line with Python Tkinter?
- How to force Tkinter text widget to stay on one line?
- How to prevent text from occupying more than one line in CSS?
- How to replace string in a large one line, text file in Linux?
- C program to Replace a word in a text by another given word
- How can we implement line wrap and word wrap text inside a JTextArea in Java?
- Converting digits to word format using switch case in C language
- How to display deleted text in HTML?
- How to set Adapter to Auto Complete Text view?

Advertisements