Here is my text file :
1 SPRITZER 999 2.500000
2 PENCILBOX 999 20.000000
3 IPHONE 90 20.000000
10 GUNNAROPTIK 1919 200.000000
The program let the user enter productID. Listed as 1,2,3,4. And delete the whole line.
I'm currently testing it in main function only. And still fail to do so.
How to do it, please help. Here is my code.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
int addProduct();
struct product
{
int quantity, reorder, i, id;
char name[20];
float price;
};
int addProduct()
{
FILE * fp;
int i=0;
struct product a;
system("cls");
char checker;
do
{
fp = fopen("addproduct.txt","a+t");
system("cls");
printf("Enter product ID : ");
scanf(" %d", &a.id);
printf("Enter product name : ");
scanf(" %s", a.name);
printf("Enter product quantity : ");
scanf(" %d", &a.quantity);
printf("Enter product price : ");
scanf(" %f", &a.price);
fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
printf("Record saved!\n\n");
fclose(fp);
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
i++;
system("cls");
}
while(checker=='Y');
if(checker == 'N')
{
fp = fopen("addproduct.txt","r");
while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
{
fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price);
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
}
fclose(fp);
}
return(0);
}
int searchProduct()
{
FILE * fp;
system("cls");
struct product a;
int productID;
fp = fopen("addproduct.txt","r");
printf("Please enter product ID : ");
scanf(" %d", &productID);
while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
{
if(a.id == productID)
{
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
}
}
fclose(fp);
return(0);
}
int reorderProduct()
{
FILE * fp;
system("cls");
struct product a;
int productQuantity;
fp = fopen("addproduct.txt","r");
printf("Please enter minimum reorder level : ");
scanf(" %d", &productQuantity);
while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
{
if(a.quantity < productQuantity)
{
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
}
}
return (0);
}
int main()
{
FILE *fp;
system("cls");
struct product a;
int counter=0;
FILE *ptr2 = fopen("file2.txt","a");
int productID;
fflush(stdin);
printf("Enter product ID you want to edit:\n");
scanf(" %d",&productID);
while(a.id!=productID)
{
fread(&a,sizeof(struct product),1,fp);
if(a.id==productID)
{
}
else
{
fwrite(&a,sizeof(struct product),1,ptr2);
}
counter++;
}
remove("addproduct.txt");
rename("file2.txt","addproduct.txt");
printf("Press any key..");
getch();
}