構造体の配列を作成し、その構造体の配列をファイルに保存してから、そのファイルを開き、そのファイルを読み取り、そのファイルの内容を構造体の配列にコピーする必要がある C でプログラムを作成しています (この「friend」と呼ばれる特定の構造体は 3 つの文字列を保持します)。ただし、配列が次のように 3 つのフレンドを保持している場合:
John Doe 234-1230 (string, string, string) <br>
Kool Kat 343-3413<br>
Suzie Q 234-1234<br>
この配列をファイルに保存し、以下の open 関数を使用して開くと、次のようになります。
Joán Doe 234-2132<br>
Kool Kat 343-3413<br>
Suzie Q 234-1234<br>
また
John Doe 234-2132<br>
Kool Kat 343-3413<br>
Suz Q 234-1234<br>
ここで、1 つの文字列 (ほとんどの場合、構造体の最初の文字列) は、1 つまたは複数のランダムな文字が切り替わったものとほぼ同じです。誰でもこのエラーの原因を教えてもらえますか?
void open(friend* book, int* size){
FILE *pRead;
char address[100];
char answer = 'a';
printf("\nWARNING: Any unsaved data in the current phonebook will be lost!");
printf("\nType the file-name you would like to open(press '1' for the default location):");
scanf("%s", &address);
if(strcmp(address, "1") == 0){
strcpy(address, "default.dat");
}
pRead = fopen(address, "r");
if(pRead == NULL){
printf("\nFile not opened\n");
}else{
int counter = 0;
while(!feof(pRead)){
fscanf(pRead, "%s%s%s", book[counter].pFName, book[counter].pLName, book[counter].pNumber);
counter++;
realloc(book, sizeof(friend) * counter);
}
*size = counter;
fclose(pRead);
printf("\n%s has been loaded into the program!", address);
}
}
その他の情報: 同じファイルに対してこの関数を呼び出し続けると、最終的には正しい文字列が生成されるため、保存関数が正しいと思われます。これはメモリ割り当てと関係がありますか?
ここに私の構造体コードがあります:
typedef struct Contact{ //creates a struct that holds three strings (first name, last name, phone number) (can be referred to as either Contact or friend
char pFName[20]; //first name of friend
char pLName[20]; //last name of contact
char pNumber[12]; //phone number of contact
}friend;