I have a problem to update or delete the record from that file. so i request please send me the code of updating & deleting the record from the file......
please guys help me
thank you
please guys help me
thank you
#include<conio.h>
#include<stdio.h>
void main()
FILE *fp;
float price,value;
int number,quantity,i;
char item[10],filename[10];
printf("input file name\n");
scanf("%s",filename);
fp=fopen(filename,"w");
printf("Item name Number Price Quantity\n");
for(i=1;i<=3;i++)
{
fscanf(stdin,"%s %d %f %d",item,&number,&price,&quantity);
fprintf(fp,"%s %d %.2f %d",item,number,price,quantity);
}
fclose(fp);
fprintf(stdout,"\n\n");
fopen(filename,"r");
printf("Item name Number Price Quantity value\n");
for(i=1;i<=3;i++)
{
fscanf(fp,"%s %d %f %d",item,&number,&price,&quantity);
value=price*quantity;
fprintf(stdout,"%-8s %7d %8.2f %8d 11.2%f\n",item,number,price,quantity,value);
}
fclose(fp);
}
delete algorithm:
Repeat for each line in the input file {
Read the next line from the input file.
If this is not the line to be deleted {
Write the line to the output file.
}
}
update algorithm:
Repeat for each line in the input file {
Read the next line from the input file.
If this is not the line to be updated {
Write the line to the output file.
}
Else {
Modify the line as per the desired update.
Write the modified line to the output file.
}
}
#include<conio.h>
#include<stdio.h>
void display();
FILE *fp;
float cost,value;
int record,quantity,i;
char ToolName[10],filename[10];
void main()
{
printf("input file name\n");
scanf("%s",filename);
fp=fopen(filename,"r+");
printf("Item name Number Price Quantity\n");
for(i=1;i<=3;i++)
{ ToolName[i]='0',record=0,cost=0,quantity=0;
//fscanf(stdin,"%s %d %f %d",ToolName,&record,&cost,&quantity);
fprintf(fp,"%s %d %.2f %d",ToolName,record,cost,quantity);
}
display();
rewind(fp);
fprintf(stdout,"%s %d %f %d \n",ToolName,record,cost,quantity);
while((fp,"%s %d %f %d",ToolName,&record,&cost,&quantity)!=0);
{
fscanf(stdin,"%s %d %f %d",ToolName,&record,&cost,&quantity);
fprintf(stdout,"%s %d %f %d \n",ToolName,record,cost,quantity);
}
fclose(fp);
display();
/*fp=fopen(filename,"r+");
rewind(fp);
while(fscanf(fp,"%s %d %f %d",ToolName,&record,&cost,&quantity)!=0);--->problem is here in while loop plz see this problem
{
fscanf(stdin,"%s %d %f %d",ToolName,&record,&cost,&quantity);
fprintf(fp,"%s %d %.2f %d",ToolName,record,cost,quantity);
}
display();*/
getch();
}
void display()
{int i;
fprintf(stdout,"\n\n");
fopen(filename,"r");
printf("Item name Number Price Quantity value\n");
for(i=1;i<=3;i++)
{
fscanf(fp,"%s %d %f %d",ToolName,&record,&cost,&quantity);
//value=cost*quantity;
fprintf(stdout,"%s %7d %f %d \n",ToolName,record,cost,quantity);
}
fclose(fp);
}
Comment