2016-08-15 8 views
1

私のcsvテキストファイルを更新する関数を作成しようとしています。 私は既にテキストファイル内にデータを持っています。選択したデータ行の1つの値の1つを変更したいだけです。私はここで何をすべきか混乱しています。 newInventory.numInstockを印刷しようとすると、メモリアドレスが与えられます。テキストファイルの値を表示するにはどうしたらいいですか?私は古いファイルを読んで、必要なものを新しいファイルにコピーする必要があることを知っています。誰かが私を助けてくれますか?Cプログラムを使用してcsvテキストファイルの特定の値を編集する

私の質問は、どのように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

'numInStock'は、2列目、4列目ではないでしょうか? (また、行にカンマがありますか?) – BLUEPIXY

+0

あなたのコメントに混乱します。 numInStockは4番目の列の位置にあります。テキストファイルではカンマで区切られています。 – JDoe

+0

'sscanf(行、"%i%i "、&newInventory.productNumber、&newInventory.numInStock)'は考慮していません。 – BLUEPIXY

答えて

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); 
    } 
} 
+0

私を助ける時間をとってくれてありがとう!ほんとうにありがとう! – JDoe

関連する問題