我有一个宠物店库存程序的功能。到目前为止,它将列出库存,并将项目添加到库存。现在我正在尝试通过它的 productNumber 删除一个项目(csv 文本文件中的第一个值存储)。我改变了我的代码,我只需要一点帮助来解决这个问题。我需要它来扫描 productNumber 并按其产品编号删除该行。
问题:如何获得在文本文件中查找 productNumber 的条件,以便删除文本文件中的该行。
我需要一些帮助!我有一个设置为以下结构的 csv 文本文件:
struct inventory_s
{
int productNumber;
float mfrPrice;
float retailPrice;
int numInStock;
char liveInv;
char productName[PRODUCTNAME_SZ +1];
};
/*Originalfile I'm trying to copy and delete from looks like*/
1000,1.49,3.79,10,0,Fish Food
2000,0.29,1.59,100,1,AngelFish
2001,0.09,0.79,200,1,Guppy
5000,2.40,5.95,10,0,Dog Collar Large
6000,49.99,129.99,3,1,Dalmation Puppy
/*function looks like*/
int deleteProduct(void)
{
struct inventory_s newInventory;
char line[50];
//int del_line, temp = 1;
FILE* originalFile = fopen("inventory.txt", "r"); //opens and reads file
FILE* NewFile = fopen("inventoryCopy.txt", "w"); //opens and writes file
if(originalFile == NULL || NewFile == NULL)
{
printf("Could not open data file\n");
return -1;
}
printf("Please enter the product number to delete:");
sscanf(line," %i", &newInventory.productNumber);
while(fgets(line, sizeof(line), originalFile) !=NULL)
{
if (!(&newInventory.productNumber))
{
fputs(line, NewFile);
}
}
fclose(originalFile);
fclose(NewFile);
return 0;
}
/*Input from user: 1000*/
/* What needs to happen in Newfile*/
2000,0.29,1.59,100,1,AngelFish
2001,0.09,0.79,200,1,Guppy
5000,2.40,5.95,10,0,Dog Collar Large
6000,49.99,129.99,3,1,Dalmation Puppy