This is the save module :
void insertFile()
{
record p;
FILE *ptr;
int choice;
do
{
if((ptr=fopen("PRODUCTS2.txt","ab"))==NULL)
{
printf("File cannot be opened");
}
else
{
printf ("Enter product ID\n");
scanf ("%s",&p.id);
printf ("Enter Product name\n");
scanf("%s",&p.name);
printf ("Enter product price\n");
scanf("%f",&p.price);
printf ("Enter product category\n");
scanf ("%s",&p.category);
printf("Enter the quantity\n");
scanf("%i",&p.quantity);
if(!feof(stdin))
{
fprintf(ptr,"%s %s %f %s %i\t\n",p.id,p.name,p.price,p.category,p.quantity);
fclose(ptr);
}
printf("Enter 1 to enter another record\nEnter 9 to EXIT\n");
scanf("%i",&choice);
}
}while(choice!=9);
}
And that works fine, but the search doenst :
void searchFile()
{
int choice;
FILE *ptr;
record p;
char search[20];
if((ptr=fopen("PRODUCTS2.txt","r"))==NULL)
{
printf("File cud not be opened\n");
}
else
{
printf("Enter name of product\n");
scanf("%s",&search);
while(!feof(ptr))
{
if(strcmp(p.name,search)==0)
{
printf("%i%f%i",p.id,p.price,p.quantity);
fscanf(ptr,"%i%f%i",p.id,p.price,p.quantity);
}
else
{
printf("Product not found\n");
}
}
}
}
If anyone is wondering , record is a datatype established earlier.
struct Record
{
char id[10];
char name[20];
float price;
char category[15];
int quantity;
};


Sign In
Create Account


Back to top









