我是一名学生tryig,要打印我已插入txt文档中的6行 test.txt
测试文件是的纯文本格式,.txt
带有以下文本,均位于单独的行中:
Nikolaj
Ljorring
m
20
182
200
但是,我需要将加载的数据放入一个看起来像这样的结构中:
struct profile_info
{
char first_name[30];
char last_name[30];
char gender;
int age;
int height;
double weight;
};
加载器/打印功能如下所示:
void user_profile_loader(struct profile_info user_profile)
{
FILE *file_pointer;
file_pointer = fopen("test.txt", "r");
fscanf(file_pointer, "%s", &user_profile.first_name);
fscanf(file_pointer, "%s", user_profile.last_name);
fscanf(file_pointer, "%c", &(user_profile.gender));
fscanf(file_pointer, "%d", &(user_profile.age));
fscanf(file_pointer, "%d", &(user_profile.height));
fscanf(file_pointer, "%d", &(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);
fclose(file_pointer);
}
但是,我的输出看起来像这样:
Nikolaj
Ljorring
(wierd C with a line beneath it)c [So a wierd C followed by a normal lowercase c]
10
5
0.000000
fscanf(file_pointer, "%s", &user_profile.first_name);
^ no need of & here
和这里 -
fscanf(file_pointer, "%d", &(user_profile.weight));
您可以使用%d
读取double
值。你把它传递调用错误的参数UB。用%lf
在这里。
在您的printf
-
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);
什么\n€c
啊 您应该使用说明符%c
。
注意-您应检查fopen
和的返回fscanf
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句