source+code+samplesThis is a featured page

#include <stdio.h>
#include <conio.h>
#include <process.h>
struct sales
{
int pid;
char name[20];
int quality;
float price;
}product;
void main()
{
int flag, opt;
int id;
FILE *fp, *fp_m;
do
{
flag = 0;
clrscr();
printf("**** Sales Records ****\n");
fp = fopen("sales.dat", "r");
if (fp==NULL)
printf("\nSales data file not created.");
else
{
while ( !feof(fp) )
{
if (flag==0)
{
printf("\nID\tNAME\t\tQuality\t\tPrice\n");
flag=1;
}
fscanf(fp, "%d %s %d %f ",&product.pid, product.name,
&product.quality, &product.price);
printf("%d\t%s\t\t%d\t\t%.2f\n",product.pid, product.name,
product.quality, product.price);
}
fclose(fp);
}
if (flag==0)
printf("\nNo records entered.");
printf("\n\n**** Menu ****\n");
printf("1) Insert New Record\n");
printf("2) Modify a Record\n");
printf("3) Delete a Record\n");
printf("4) Reset File\n");
printf("5) Exit\n");
printf("Enter your choice: ");
scanf("%d", &opt);
switch (opt)
{
case 1:
/* Inserting New Record */
printf("\nEnter details of New Record:\n");
printf("* Product ID: ");
scanf("%d", &product.pid);
printf("* Product Name: ");
scanf("%s", product.name);
printf("* Product Quality: ");
scanf("%d", &product.quality);
printf("* Product Price: ");
scanf("%f", &product.price);
fp = fopen("sales.dat", "a");
fprintf(fp, "%d %s %d %f ", product.pid, product.name,
product.quality, product.price);
printf("\n\nRecord added successfully...");
fclose(fp);
break;
case 2:
/*Modifying a Record*/
flag = 0;
printf("\n\nEnter the Product ID of the record to be modified: ");
scanf("%d", &id);
fp = fopen("sales.dat", "r");
if (fp==NULL)
printf("\nSales data file not created.");
else
{
fp_m = fopen("temp.dat", "w");
if (fp_m == NULL)
{
printf("\n! File Creation error...\n");
fclose(fp_m);
printf("Program will now terminate...\n");
getch();
exit(0);
}
while ( !feof(fp) )
{
fscanf(fp, "%d %s %d %f ",&product.pid, product.name,
&product.quality, &product.price);
if (product.pid == id)
{
printf("\nEnter the new info: ");
printf("\nProduct ID: %d", id);
printf("\nProduct Name: ");
scanf("%s", product.name);
printf("Product Quality: ");
scanf("%d", &product.quality);
printf("Price: ");
scanf("%f", &product.price);
flag = 1;
}
fprintf(fp_m, "%d %s %d %f ", product.pid, product.name,
product.quality, product.price);
}//End of while
fclose(fp_m);
}//End of else
fclose(fp);
remove("sales.dat");
rename("temp.dat", "sales.dat");
printf("\nRecord modified successfully...");
break;
case 3:
/* Deleting a Record */
flag = 0;
printf("\n\nEnter the Product ID to be deleted: ");
scanf("%d", &id);
fp = fopen("sales.dat", "r");
if (fp==NULL)
printf("\nSales data file not created.");
else
{
fp_m = fopen("temp.dat", "w");
if (fp_m == NULL)
{
printf("\n! File Creation error...\n");
fclose(fp);
fclose(fp_m);
printf("Program will now terminate...\n");
getch();
exit(0);
}
while ( !feof(fp) )
{
fscanf(fp, "%d %s %d %f ",&product.pid, product.name,
&product.quality, &product.price);
if (product.pid == id)
flag = 1;
else
{
fprintf(fp_m, "%d %s %d %f ", product.pid, product.name,
product.quality, product.price);
}
}//End of while
fclose(fp_m);
}//End of else
fclose(fp);
if (flag==1)
{
printf("\nRecord deleted successfully...");
remove("sales.dat");
rename("temp.dat", "sales.dat");
fp = fopen("sales.dat", "r");
fseek(fp, 0, 2);
if (ftell(fp)==0)
{
fclose(fp);
remove("sales.dat");
}
else
fclose(fp);
}
else
{
printf("\nRecord Not Found...");
remove("temp.dat");
}
break;
case 4:
/* Reset File */
remove("sales.dat");
printf("\nFile reset successfully...");
break;
case 5:
printf("\n\n! Thank You !");
break;
default:
printf("\n\n! Invalid Option !");
}
getch();
}while (opt!=5);
}


sharma'z
sharma'z
Latest page update: made by sharma'z , Nov 23 2007, 6:08 AM EST (about this update About This Update sharma'z Edited by sharma'z

452 words added

view changes

- complete history)
Keyword tags: None
More Info: links to this page

Anonymous  (Get credit for your thread)


There are no threads for this page.  Be the first to start a new thread.