尝试为我的结构分配输入字符时,程序崩溃
When trying to assing a input character to my struct the program chrashes
作为编程的新学生,我正在尝试制作一个程序,该程序将从用户那里获取输入并将其保存到一个结构中,该结构稍后将保存到一个文件中,除了用户输入部分之外的所有内容都可以正常工作,但是这里的前两部分按预期工作(名字和姓氏是数组)
printf("And what about down below? are you a man or a woman? <M/F>\n");
scanf(" %c", user_profile.gender);
是让我的程序崩溃的部分
该结构在任何函数之外赋值,以使其在此处成为全局
struct profile_info
{
char first_name[30];
char last_name[30];
char gender;
int age;
int height;
double weight;
};
最后我在我的 main() 中创建了一个占位符,但我不知道这是否需要
strcpy(user_profile.first_name, "placeholder");
strcpy(user_profile.last_name, "placeholder");
user_profile.gender = 't';
user_profile.age = 5;
user_profile.height = 5;
user_profile.weight = 5;
赋值发生在一个函数内部,该函数由 main() 调用的函数调用,如果有任何相关性的话
也是占位符,是在main中创建的
** 更新 **
问题已解决(看答案1)
然而,当尝试使用 facanf 扫描回文件时出现了一个新问题,结果如下
我试过使用 fscanf()
但是前两行没有用
/* this function will load the user profile if such a profile exists */
void user_profile_loader(struct profile_info user_profile)
{
FILE *file_pointer;
int i;
file_pointer = fopen("test.txt", "r");
fscanf(file_pointer, user_profile.first_name);
fscanf(file_pointer, user_profile.last_name);
fscanf(file_pointer, &(user_profile.gender));
fscanf(file_pointer, &(user_profile.age));
fscanf(file_pointer, &(user_profile.height));
fscanf(file_pointer, &(user_profile.weight));
printf("%s \n%s \n€c \n%d \n%d \n%lf", user_profile.first_name, user_profile.last_name,
user_profile.gender, user_profile.age, user_profile.height, user_profile.weight);
}
但是,我需要帮助指定应该读取哪些行(1 到 6),我首先尝试使用
/* this function will load the user profile if such a profile exists */
void user_profile_loader(struct profile_info user_profile)
{
FILE *file_pointer;
int i;
file_pointer = fopen("test.txt", "r");
fscanf(file_pointer, 1, user_profile.first_name);
fscanf(file_pointer, 2, user_profile.last_name);
fscanf(file_pointer, 3, &(user_profile.gender));
fscanf(file_pointer, 4, &(user_profile.age));
fscanf(file_pointer, 5, &(user_profile.height));
fscanf(file_pointer, 6, &(user_profile.weight));
printf("%s \n%s \n€c \n%d \n%d \n%lf", user_profile.first_name, user_profile.last_name,
user_profile.gender, user_profile.age, user_profile.height, user_profile.weight);
}
两个都不行,当前的(第一个)给我一个错误
In file included from introduktion.c:1:0:
(my directory) stdio.h:385:15: note: expected 'const char * restrict' but argument is of type 'int'
int __cdec1 fscanf(FILE * __restrict__ _File,const char * __restrict__ _Format,...) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
和
传递 fscanf 的参数 2 从整数生成指针而不进行强制转换
所以它不明白它得到了什么输入?
在您的代码中,
scanf(" %c", user_profile.gender);
应该是
scanf(" %c", &(user_profile.gender));
^
您需要提供变量的 地址 作为格式说明符的参数。
阅读 man page 了解有关 scanf()
的更多信息。
作为编程的新学生,我正在尝试制作一个程序,该程序将从用户那里获取输入并将其保存到一个结构中,该结构稍后将保存到一个文件中,除了用户输入部分之外的所有内容都可以正常工作,但是这里的前两部分按预期工作(名字和姓氏是数组)
printf("And what about down below? are you a man or a woman? <M/F>\n");
scanf(" %c", user_profile.gender);
是让我的程序崩溃的部分
该结构在任何函数之外赋值,以使其在此处成为全局
struct profile_info
{
char first_name[30];
char last_name[30];
char gender;
int age;
int height;
double weight;
};
最后我在我的 main() 中创建了一个占位符,但我不知道这是否需要
strcpy(user_profile.first_name, "placeholder");
strcpy(user_profile.last_name, "placeholder");
user_profile.gender = 't';
user_profile.age = 5;
user_profile.height = 5;
user_profile.weight = 5;
赋值发生在一个函数内部,该函数由 main() 调用的函数调用,如果有任何相关性的话
也是占位符,是在main中创建的
** 更新 **
问题已解决(看答案1) 然而,当尝试使用 facanf 扫描回文件时出现了一个新问题,结果如下
我试过使用 fscanf()
但是前两行没有用
/* this function will load the user profile if such a profile exists */
void user_profile_loader(struct profile_info user_profile)
{
FILE *file_pointer;
int i;
file_pointer = fopen("test.txt", "r");
fscanf(file_pointer, user_profile.first_name);
fscanf(file_pointer, user_profile.last_name);
fscanf(file_pointer, &(user_profile.gender));
fscanf(file_pointer, &(user_profile.age));
fscanf(file_pointer, &(user_profile.height));
fscanf(file_pointer, &(user_profile.weight));
printf("%s \n%s \n€c \n%d \n%d \n%lf", user_profile.first_name, user_profile.last_name,
user_profile.gender, user_profile.age, user_profile.height, user_profile.weight);
}
但是,我需要帮助指定应该读取哪些行(1 到 6),我首先尝试使用
/* this function will load the user profile if such a profile exists */
void user_profile_loader(struct profile_info user_profile)
{
FILE *file_pointer;
int i;
file_pointer = fopen("test.txt", "r");
fscanf(file_pointer, 1, user_profile.first_name);
fscanf(file_pointer, 2, user_profile.last_name);
fscanf(file_pointer, 3, &(user_profile.gender));
fscanf(file_pointer, 4, &(user_profile.age));
fscanf(file_pointer, 5, &(user_profile.height));
fscanf(file_pointer, 6, &(user_profile.weight));
printf("%s \n%s \n€c \n%d \n%d \n%lf", user_profile.first_name, user_profile.last_name,
user_profile.gender, user_profile.age, user_profile.height, user_profile.weight);
}
两个都不行,当前的(第一个)给我一个错误
In file included from introduktion.c:1:0:
(my directory) stdio.h:385:15: note: expected 'const char * restrict' but argument is of type 'int'
int __cdec1 fscanf(FILE * __restrict__ _File,const char * __restrict__ _Format,...) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
和
传递 fscanf 的参数 2 从整数生成指针而不进行强制转换 所以它不明白它得到了什么输入?
在您的代码中,
scanf(" %c", user_profile.gender);
应该是
scanf(" %c", &(user_profile.gender));
^
您需要提供变量的 地址 作为格式说明符的参数。
阅读 man page 了解有关 scanf()
的更多信息。