1

我正在尝试创建一个更新我的 csv 文本文件的函数。我已经拥有的文本文件里面有数据。我只想更改所选数据行之一中的值之一。我有点困惑在这里做什么。当我尝试打印出 newInventory.numInstock 时,它给了我内存地址。如何让它显示文本文件值?我知道我需要阅读旧文件,然后将我需要的内容复制到新文件中。有人可以帮我解决这个问题吗?

我的问题是如何让它修改 numInStock?我希望能够用这个功能改变它。

/*here's my structure*/

struct inventory_s
{
    int productNumber;
    float mfrPrice;
    float retailPrice;
    int numInStock;// 4th column
    char liveInv;
    char productName[PRODUCTNAME_SZ +1];
};

/* My text file looks something 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

/*Here's my function so far*/

int updateStock(void)
{
    struct inventory_s newInventory;
    int productNumber;
    char line[50];

    FILE* originalFile = fopen("stuff.txt", "r"); //opens and reads file
    FILE* NewFile = fopen("stuffCopy.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 modify:");
    scanf(" %i", &productNumber);

    printf("Current value is %i; please enter new value:", &newInventory.numInStock );
    while(fgets(line, sizeof(line), originalFile) != NULL)
    {
        sscanf(line, "%d,%*f,%*f,%i", &newInventory.productNumber, &newInventory.mfrPrice, &newInventory.retailPrice, &newInventory.numInStock);
        if (productNumber == newInventory.productNumber)
        {
            fputs(line, NewFile);
            //fscanf(NewFile, "%s", &newInventory.productName);
            printf(" %i",newInventory.numInStock);
        }
    }

    fclose(originalFile);
    fclose(NewFile);
    // remove("stuff.txt");
    //rename("stuffCopy.txt", "inventory.txt");

    return 0;
}

到目前为止,我可以打印出我要访问的行。我需要它来访问结构中的一个值并只显示那个值。然后我需要将其更改为来自用户的新值。

4

1 回答 1

1

像这样修复(这将是你的帮助。)

printf("Please enter the product number to modify:");
scanf("%i", &productNumber);

while(fgets(line, sizeof(line), originalFile) != NULL)
{
    sscanf(line, "%i", &newInventory.productNumber);
    if (productNumber == newInventory.productNumber)
    {
        sscanf(line, "%i,%f,%f,%i,%c,%[^\n]", &newInventory.productNumber, 
                                              &newInventory.mfrPrice,
                                              &newInventory.retailPrice,
                                              &newInventory.numInStock,
                                              &newInventory.liveInv,
                                              newInventory.productName);
        printf("Current value is %i; please enter new value:", newInventory.numInStock );
        scanf("%i", &newInventory.numInStock );
        fprintf(NewFile, "%i,%f,%f,%i,%c,%s\n",newInventory.productNumber, 
                                               newInventory.mfrPrice,
                                               newInventory.retailPrice,
                                               newInventory.numInStock,
                                               newInventory.liveInv,
                                               newInventory.productName);
    }
    else
    {
        fputs(line, NewFile);
    }
}
于 2016-08-15T05:03:40.767 回答