【问题标题】:Editing a specific value in a csv text file through C programming通过 C 编程编辑 csv 文本文件中的特定值
【发布时间】:2016-08-15 03:52:26
【问题描述】:

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

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

【问题讨论】:

  • numInStock 是第 2 列,而不是第 4 列? (也行有逗号?)
  • 我对您的评论感到困惑。 numInStock 位于第四列位置。它们在文本文件中用逗号分隔。
  • sscanf(line, "%i %i", &newInventory.productNumber, &newInventory.numInStock) 不考虑是他们。
  • 我明白你的意思了。那么我需要扫描所有这些吗?
  • "%i %i" 表示第一列和第二列用空格分隔(类型为int)。

标签: c csv copy scanf file-pointer


【解决方案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);
    }
}

【讨论】:

  • 感谢您花时间帮助我!我真的很感激!
猜你喜欢
  • 2012-03-09
  • 2011-06-26
  • 2010-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多