0% found this document useful (0 votes)
105 views

C Program To Read Content of A File and Display It

The document provides code samples to demonstrate reading and writing files in C programming. It includes a program to read the contents of a file and display it, a program to create a new file and write data to it by taking user input, and a program to copy the contents of one file into another file by reading from the first file and simultaneously writing to the second file. The document also includes a short example to print the contents of a file in reverse order.

Uploaded by

Pallab Datta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views

C Program To Read Content of A File and Display It

The document provides code samples to demonstrate reading and writing files in C programming. It includes a program to read the contents of a file and display it, a program to create a new file and write data to it by taking user input, and a program to copy the contents of one file into another file by reading from the first file and simultaneously writing to the second file. The document also includes a short example to print the contents of a file in reverse order.

Uploaded by

Pallab Datta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C Program to Read content of a File and

Display it
Below is a simple program to read the content of any file and then print it on the output
screen.
#include<stdio.h>
#include <stdlib.h> // for exit() function

int main()
{
char c[1000];
FILE *fptr;

if ((fptr = fopen("studytonight.txt", "r")) == NULL)


{
printf("Error! opening file");
// exit from program if file pointer returns NULL.
exit(1);
}

// read the text until newline


fscanf(fptr,"%[^\n]", c);

printf("Data from the file:\n%s", c);


fclose(fptr);

return 0;
}

C Program to create a File & write Data in


it
Below is a program to create a new file and then storing information in it.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
char name[20];
int age;
float salary;

/* open for writing */


fptr = fopen("emp.txt", "w");

if (fptr == NULL)
{
printf("File does not exist.\n");
return;
}
printf("Enter the name:\n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);

printf("Enter the age:\n");


scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);

printf("Enter the salary:\n");


scanf("%f", &salary);
fprintf(fptr, "Salary = %.2f\n", salary);

fclose(fptr);
}
C Program to copy content of one File
into another File
We already know how to open a file, read contents of a file and write into a file. So in this
program, we will read from one file and simultaneously write into the other file, till we reach
end of first file.
#include<stdio.h>
#include<stdio.h>

void main()
{
/*
File_1.txt is the file with content and,
File_2.txt is the file in which content of File_1
will be copied.
*/
FILE *fp1, *fp2;
char ch;
int pos;

if ((fp1 = fopen("File_1.txt", "r")) == NULL)


{
printf("\nFile cannot be opened.");
return;
}
else
{
printf("\nFile opened for copy...\n ");
}
fp2 = fopen("File_2.txt", "w");
fseek(fp1, 0L, SEEK_END); // File pointer at end of file
pos = ftell(fp1);
fseek(fp1, 0L, SEEK_SET); // File pointer set at start
while (pos--)
{
ch = fgetc(fp1); // Copying file character by character
fputc(ch, fp2);
}
fcloseall();
}

a) Create a text file 'input.txt'


b) Print the contents of file in reverse order

Solution:

#include<stdio.h>
int main()
{
FILE *fp;
char ch;
int i,pos;
fp=fopen("input.txt","r");
if(fp==NULL)
{
printf("File does not exist..");
}
fseek(fp,0,SEEK_END);
pos=ftell(fp);
//printf("Current postion is %d\n",pos);
i=0;
while(i<pos)
{
i++;
fseek(fp,-i,SEEK_END);
//printf("%c",fgetc(fp));
ch=fgetc(fp);
printf("%c",ch);
}
return 0;
}

Output:

Original Content in File

Print content of file in reverse order.

You might also like