通过C编程在csv文本文件中编辑特定值

时间:2016-08-15 03:52:27

标签: c csv copy scanf file-pointer

我正在尝试创建一个更新我的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;
}

到目前为止,我打印出我正在尝试访问的行。我需要它才能访问结构中的一个值并仅显示一个值。然后我需要将其更改为用户的新值。

1 个答案:

答案 0 :(得分: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);
    }
}