Program
Name:- Write a ‘C’ Program to read data
from a text file & display data on the screen, if file is not exist then
create it.
#include<stdio.h>
#include<conio.h>
void
main()
{
FILE
*fp;// FILE OPEN
char ch;
clrscr();
fp=fopen("a.txt","r");//FILE
OPEN FOR READ MODE
if(fp==NULL)
{
printf("\n
Unable To Open a File");
}
while((ch=fgetc(fp))!=EOF)
{
putchar(ch);
}
fclose(fp);
getch();
}
/*output
all
indians are my brothers and sisters */
Program Name:- Write a ‘C’ Program to append the containt of one file at
the end of another file.
#include<stdio.h>
#include<conio.h>
void
main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("b.txt","a");
if(fp==NULL)
{
printf("\n
Unable to open");
}
while((ch=getchar())!=EOF)
{
fputc(ch,fp);
}
fclose(fp);
getch();
}
/* output
india is
my country
all
indians are may brothers and sisters.^Z^Z*/
Program Name:- Write a ‘C’ Program to copy the contents of one file into
another file
#include<stdio.h>
#include<conio.h>
void
main()
{
FILE
*fs,*ft;
char ch;
fs=fopen("a.txt","r");
if(fs==NULL)
{
printf("\nunable
to open file");
}
ft=fopen("b.txt","w");
if(ft==NULL)
{
printf("cannot
open target file");
}
while((ch=getc(fs))!=EOF)
{
fputc(ch,ft);
}
fclose(fs);
fclose(ft);
getch();
}
After
copying
Program Name:- Write a ‘C’ Program to store the records of student
(stud_no, Stud_name,
stud_addr, stud_Percentage) in a file using structure.
#include<stdio.h>
#include<conio.h>
void main()
{
struct stud
{
int sno;
float per;
char name[20],add[20];
}s;
int i,n;
char ch;
FILE *fp;
fp=fopen("a.txt","w");
if(fp==NULL)
{
printf("\nUnable to open file!");
}
else
{
printf("\nEnter the stud no:->");
scanf("%d",&s.sno);
printf("\nEnter the stud name:->");
scanf("%s",s.name);
printf("\nEnter the stud add:->");
scanf("%s",s.add);
printf("\nEnter the stud per:->");
scanf("%f",&s.per);
fprintf(fp,"%d\n%s\n%s\n%f",s.sno,s.name,s.add,s.per);
}
fclose(fp);
getch();
}
/* output
Enter the stud no:->1
Enter the stud name:->amol
Enter the stud add:->baramati
Enter the stud per:->80*/
Program Name:- Write a
‘C’ Program to count the number of characters, number of words and number of
lines from a text file and display the result
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
int noc=0,now=0,nol=0,not=0;
clrscr();
fp=fopen("a.txt","r");
if(fp==NULL)
{
printf("\n unable to open file");
}
while((ch=fgetc(fp))!=EOF)
{
if(ch==EOF)
{
break;
}
noc++;
if(ch=='
')
{
now++;
}
if(ch=='\n')
{
nol++;
}
if(ch=='\t')
{
not++;
}
}
fclose(fp);
printf("\n the no of characters are %d",noc);
printf("\n the no of words are %d",now+nol);
printf("\n the no of lines are %d",nol);
printf("\n the no of tabs are %d",not);
getch();
}
the no of characters are 20
the no of words are 3
the no of lines are 2
the no of tabs are 0
Program Name:- Write a ‘C’ program to do following
a.
Create a text file
‘input.txt’
b.
Print the contents of
file in reverse order
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch,s[100];
int i=0;
clrscr();
printf("Enter the content of
file\n");
fp=fopen("E:\input.txt","w");
while((ch=getchar())!=EOF)
{
fputc(ch,fp);
}
fclose(fp);
fp=fopen("E:\input.txt","r");
while((s[i]=fgetc(fp))!=EOF)
{
i++;
}
while(i>=0)
{
printf("%c",s[i]);
i--;
}
fclose(fp);
getch();
}
/* output
Enter the content of file
india is my country
all indians^Z
snaidni lla
yrtnuoc ym si aidni
*/