我正在编写一个处理文件的程序。我需要能够将数据作为结构输入,并最终将其读出。我目前遇到的问题是这段代码:
typedef struct {
char* name;
.....
}employeeRecord;
employeeRecord record;
char name[50];
if(choice == 1)
{
/*Name*/
printf("\nEnter the name:");
fgets(name,50,stdin);
record.nameLength = strlen(name) -1;
record.name = malloc(sizeof(char)*record.nameLength);
strcpy(record.name,name);
/*Other data, similar format...*/
例如,如果我想要姓名地址和电话号码,并连续询问每个人(所以地址与上面几乎相同,除了用地址替换“姓名”),我发现它会跳过输入。我的意思是,我没有机会输入它。输出实际上是 Enter the name: Enter the address: (这里是提示我输入的地方)