1

csv テキスト ファイルを更新する関数を作成しようとしています。私が持っているテキスト ファイルには、既にデータが含まれています。選択したデータ行の 1 つの値を変更したいだけです。私はここで何をすべきかについてちょっと混乱しました。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 つにアクセスし、その 1 つだけを表示するために必要です。次に、ユーザーから新しい値に変更する必要があります。

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 に答える