我目前在Windows XP中使用MinGW。如何在特定行編輯文本文件C
我編寫了一個程序,它接受用戶的輸入,並將它們放入.txt文件通過
typedef struct data_base{
char name[254];
int age;
int postalcode;
struct data_base *next;
}person;
我想知道是否有編輯.txt文件的數據的方法。
例如,在.txt文件,我有3套基於用戶輸入的數據:
Steven //name
19 //age
1100 //postal code
Jack
24
2203
Mary
21
0109
我會問哪一組,他希望編輯數據的用戶。然後,在完成編輯的數據之後,我希望在用戶選擇的特定集合的.txt文件中覆蓋該數據。
#include <stdio.h>
#include <stdlib.h>
typedef struct data_base{
char name[254];
int age;
int postalcode;
struct data_base *next;
}person;
void read()
{
person *curr[20];
int count = 0;
FILE *f;
int editchoice = 0;
f = fopen("personfile.txt","r+");
// Read the data in the file based on user's input
//Display the names: 1. Steven 2.Jack 3.Mary
printf("Editing Whose Data?: \n");
scanf("%d",&editchoice);
printf("New name: \n");
scanf("%s",&curr[editchoice]->name);
fprintf(f,"%s\n",curr[editchoice]->name);
printf("New age: \n");
scanf("%d",&curr[editchoice]->age);
fprintf(f,"%d\n",curr[editchoice]->age);
printf("New name: \n");
scanf("%d",&curr[editchoice]->postalcode);
fprintf(f,"%d\n",curr[editchoice]->postalcode);
}
我預期的數據將被覆蓋,但情況並非如此。 (對不起,初學者。)
你能顯示你試過的代碼嗎? –
@PaulGriffiths好吧。 – user2882762
爲什麼你期望文件被更新?你沒有更新它。爲了更新文件,您需要將文件定位到要更新的記錄並使用'fwrite'覆蓋它。 –