我正在用 C 语言编写一个程序,在该程序中我需要创建一个结构数组,将该结构数组保存到一个文件中,然后打开该文件,读取该文件,并将该文件的内容复制到一个结构数组中(这名为“friend”的特定结构包含三个字符串)。但是,如果数组包含三个这样的朋友:
John Doe 234-1230 (string, string, string) <br>
Kool Kat 343-3413<br>
Suzie Q 234-1234<br>
一旦我将此数组保存到一个文件并使用下面的打开函数打开它,我就会得到类似的东西:
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>
其中一个字符串(几乎总是结构中的第一个字符串)与一个或多个随机字符几乎完全相同。谁能告诉我是什么导致了这个错误?
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;