Chapter File input Output
Q. Write a program to save the data in a file.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
FILE *fp;
clrscr();
fp=fopen("c:\\myfile.txt","a");
printf("Enter Two Numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf(" %d + %d = %d\n",a,b,c);
fprintf(fp,"%d + %d = %d\n",a,b,c);
printf("The File Has Been Saved");
fclose(fp);
getch();
}
Q. Write a program to save the data in a file with write mode only.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
FILE *fp;
clrscr();
fp=fopen("c:\\myfile.txt","w");
printf("Enter Two Numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf(" %d + %d = %d\n",a,b,c);
fprintf(fp,"%d + %d = %d\n",a,b,c);
printf("The File Has Been Saved");
fclose(fp);
getch();
}
Q. Write a program to write and read the data in a file.
// you need to make two separate file as f1.c for write the file
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
FILE *fp;
clrscr();
fp=fopen("c:\\keshab.txt","a");
printf("Enter Two Numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf(" %d + %d = %d\n",a,b,c);
fprintf(fp,"%d %d %d\n",a,b,c);
printf("The File Has Been Saved");
fclose(fp);
getch();
}
//now save another file as f2.c and write the code as to read the file:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
FILE *fp;
clrscr();
fp=fopen("c:\\keshab.txt","r");
printf("The Data as read :\n\n");
while(fscanf(fp,"%d%d%d",&a,&b,&c)!=EOF)
printf("%d + %d = %d\n",a,b,c);
fclose(fp);
getch();
}
Q. How can I combine these two read and write program within single file.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int a,b,c;
FILE *fp;
clrscr();
k: printf(“\nEnter 1 for Write a file”);
printf(“ \nEnter 2 for Read a file”);
printf(“ \nEnter 3 for Exit”);
printf(“ \nEnter Your Choice”);
scanf(“%d”,&i);
switch(i)
{
case 1: fp=fopen("c:\\keshab.txt","a");
printf("Enter Two Numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf(" %d + %d = %d\n",a,b,c);
fprintf(fp,"%d %d %d\n",a,b,c);
printf("The File Has Been Saved");
fclose(fp);
break;
case 2: fp=fopen("c:\\keshab.txt","r");
printf("The Data as read :\n\n");
while(fscanf(fp,"%d%d%d",&a,&b,&c)!=EOF)
printf("%d + %d = %d\n",a,b,c);
fclose(fp);
break;
case 3: exit(0);
default : printf(“Invalid choice”);
goto k;
}
getch();
}
Q. Write a program to entry, show and search a data as a dbms program.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,o=0,count=0;
char a[20],b[10],c[10],n[20],ch;
FILE *fp;
clrscr();
k: printf("\nEnter 1 for Entry");
printf("\nEnter 2 for Read ");
printf("\nEnter 3 for Search");
printf(" \nEnter 4 for Exit");
printf(" \nEnter Your Choice");
scanf("%d",&i);
switch(i)
{
case 1: fp=fopen("c:\\mytable.txt","a");
while (ch!='q')
{
printf("Enter the Name");
fflush(stdin);
scanf("%s",a);
printf("Enter the Address");
fflush(stdin);
scanf("%s",b);
printf("Enter the Phone No");
fflush(stdin);
scanf("%s",c);
strupr(a);
strupr(b);
strupr(c);
fprintf(fp,"%s %s %s\n",a,b,c);
printf("The Record Has Been Saved");
printf("\n\nAnother Record press any key or hit q");
fflush(stdin);
ch=getch();
}
fclose(fp);
goto k;
case 2: fp=fopen("c:\\mytable.txt","r");
printf("\n\nName\t\t\tAddress\t\t\tPhone No\t\n");
printf("--------------------------------------------------------------\n");
while(fscanf(fp,"%s%s%s",a,b,c)!=EOF)
{
printf("%s\t\t\t%s\t\t\t%s\t\t\t\n",a,b,c);
if (count>=20)
{
getch();
}
count++;
}
fclose(fp);
goto k;
break;
case 3: fp=fopen("c:\\mytable.txt","r");
printf("Enter the name to Search");
scanf("%s",n);
strupr(n);
printf("\n\nName\t\t\tAddress\t\t\tPhone No\t\n");
printf("--------------------------------------------------------------\n");
while(fscanf(fp,"%s%s%s",a,b,c)!=EOF)
{
if(strcmp(n,a)==0)
{
printf("%s\t\t\t%s\t\t\t%s\t\t\t\n",a,b,c);
o=1;
}
}
if (o==0) printf("\n Sorry Boss!!! the record is unknown\n\n\n");
fclose(fp);
goto k;
case 4: printf("\n\nBye Bye, See you Again");
getch();
exit(0);
default : printf("Invalid choice");
goto k;
}
getch();
}
No comments:
Post a Comment