To Be Familiarize With Data Files in C
- WAP to enter a sentence in a file and display its contents.
- WAP to read a file and count how many alphabets, spaces and words are there in file.
- WAP to copy the data of a file into another file and display the content of destination file
- WAP to read the information about the bie like name, lot no and cost into a file. Enter information of 4 bikes and display the total cost of all bikes.
- WAP to enter name ,age and basic salary of employees until user inputs "NO". Display record of employees from a file whose salary is less than 1000.
==================================================================================
1.) Question 1
#include <stdio.h>
#include <conio.h>
#include <process.h>
void main ()
{
FILE *fp;
char ch;
clrscr ();
fp=fopen("abc.txt","w+");
if (fp==NULL)
exit(0);
fprintf (fp,"This is Good.\n");
fclose (fp);
fp=fopen ("abc.txt","r");
if(fp==NULL)
exit (0);
while (1)
{
ch=fgetc(fp);
if (ch==EOF)
break ;
else
putchar (ch);
}
fclose (fp);
getch ();
}
==================================================================================
2.) Question 2
#include <stdio.h>
#include <conio.h>
#include <process.h>
void main ()
{
FILE *fp;
char ch;
int al=0,sp=0,wd=0;
clrscr ();
fp=fopen ("abc.txt","r");
if(fp==NULL)
exit (0);
while (1)
{
ch=fgetc(fp);
if (ch==EOF)
break ;
else
{
if ((ch>='a'&& ch<='z')||(ch>='A'&&ch<='Z'))
al++;
else if (ch==' ')
sp++;
else
wd=sp+1;
}
}
printf ("Alphabet=%d Space=%d Word=%d",al,sp,wd);
fclose (fp);
getch ();
}
==================================================================================
3.) Question 3
#include <stdio.h>
#include <conio.h>
#include <process.h>
void main ()
{
FILE *fp,*p;
char ch;
clrscr ();
fp=fopen ("abc.txt","r");
p=fopen ("xyz.txt","w");
while (1)
{
ch=fgetc(fp);
if (ch==EOF)
break ;
else
fputc (ch,p);
}
fclose (p);
fclose (fp);
p=fopen ("xyz.txt","r");
if (p==NULL)
exit (0);
while (1)
{
ch=fgetc(p);
if(ch==EOF)
break;
else
printf ("%c",ch);
}
fclose (p);
getch ();
}
==================================================================================
4.) Question 4
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <string.h>
void main ()
{
char name [10],ch;
int lot, price,i,tot=0;
FILE *fp;
clrscr ();
fp=fopen("lab134t.txt","w");
if (fp==NULL)
exit (0);
for (i=0;i<5;i++)
{
printf ("Enter name,age & salary: ");
scanf ("%s %d %d",name,&lot,&price);
fprintf (fp,"%s %d %d",name,lot,price);
fflush (stdin);
}
fclose (fp);
fp=fopen ("lab134t.txt","r");
rewind (fp);
if (fp==NULL)
exit(0);
for (i=0;i<5;i++)
{
tot+=price;
}
printf ("The total price is %d.",tot);
fclose (fp);
getch ();
}
==================================================================================
5.) Question 5
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <string.h>
void main ()
{
char name [10],ch;
int age, sal;
FILE *fp;
clrscr ();
fp=fopen("lab135t.txt","w");
if (fp==NULL)
exit (0);
do
{
printf ("Enter name,age & salary: ");
scanf ("%s %d %d",name,&age,&sal);
fprintf (fp,"%s %d %d",name,age,sal);
fflush (stdin);
printf ("Do you want more?y/n ");
scanf ("%c",&ch);
} while (ch!='n');
fclose (fp);
fp=fopen ("lab135t.txt","r");
rewind (fp);
if (fp==NULL)
exit(0);
printf ("The record of employee whose salary is less than 1000 is:\n");
while ((fscanf (fp,"%s %d %d",name,&age,&sal))!=EOF)
{
if (sal<=1000)
printf ("Name: %s\nage: %d\nsalary:%d\n ",name,age,sal);
}
fclose (fp);
getch ();
}
No comments