
- 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 print the content into the files using C language?
We can write a program in C for printing some content in to the file and print the following −
- Number of characters entered into the file.
- Reverse the characters entered into the file.
First, try to store number of characters into the file by opening the file in write mode.
For entering data into file, we use the logic as mentioned below −
while ((ch = getchar( ))!=EOF) {//after enter data press cntrl+Z to terminate fputc(ch, fp); }
With the help of ftell, rewind, fseek functions, we can reverse the content that we already entered into the file.
Example
Given below is a C program for printing some content into the file and print the number of characters and reverse the characters which are entered into the file −
#include<stdio.h> int main( ){ FILE *fp; char ch; int n,i=0; fp = fopen ("reverse.txt", "w"); printf ("enter text press ctrl+z of the end"); while ((ch = getchar( ))!=EOF){ fputc(ch, fp); } n = ftell(fp); printf ( "No. of characters entered = %d
", n); rewind (fp); n = ftell (fp); printf ("fp value after rewind = %d
",n); fclose (fp); fp = fopen ("reverse.txt", "r"); fseek(fp,0,SEEK_END); n = ftell(fp); printf ("reversed content is
"); while(i<n){ i++; fseek(fp,-i,SEEK_END); printf("%c",fgetc(fp)); } fclose (fp); return 0; }
Output
When the above program is executed, it produces the following result −
enter text press ctrl+z of the end TutorialsPoint ^Z No. of characters entered = 18 fp value after rewind = 0 reversed content is tnioPslairotuT
- Related Articles
- How to print the range of numbers using C language?
- How to print the stars in Diamond pattern using C language?
- How to print div content using jQuery?
- How to print the content of JavaScript object?
- What are the text files and binary files in C language?
- Explain the Random accessing files in C language
- Explain the custom header files in C language
- How to align text content into center using JavaScript?
- How to copy the content of a div into another div using jQuery?
- Print “Hello World” in C/C++ without using header files
- How to print a name multiple times without loop statement using C language?
- How to read file content into istringstream in C++?
- How to specify the language of the element's content in HTML?
- How to add multi-language content in HTML?
- How to align the output using justificationsin C language?

Advertisements