C Program To Read Content of A File and Display It
C Program To Read Content of A File and Display It
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;
return 0;
}
if (fptr == NULL)
{
printf("File does not exist.\n");
return;
}
printf("Enter the name:\n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
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;
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: