現在、Windows XP で MinGW を使用しています。
ユーザーの入力を受け取り、それらを.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);
}
データが上書きされると思っていましたが、そうではありません。(初心者ですみません。)