int get_name()
{
char cName[] = "hello";
int iCode, i = 0;
struct sign_in items[6];//array of six structure variables
Fpointin =fopen("namepass.txt","r");
if (Fpointin == NULL)
{
printf ("File does not exist.\n");
}
else
{
for (i=0;i<6;i++)
{
fscanf(Fpointin,"%s %d",items[i].name,items[i].password);//read all values from the file into th structure
}
printf("Here is the sign_in structure\n");//print the entirety of the sign_in structure
for (i=0;i<6;i++)
{
printf("name: %s\ncode: %d\n\n", items[i].name, items[i].password);
}
}
fclose(Fpointin);
}
大家好。所以我从一个项目中得到了这部分代码,每当我尝试运行它时它就会崩溃。我正在尝试将名称及其各自的密码从文件读取到结构中,但它不起作用。在这一fscanf
行中,我%s %d
交换了标识符并运行了,但它打印了随机的东西,这些东西甚至没有接近文件中的内容。有任何想法吗?
[评论更新:]
struct sign_in
{
int password; //The password for each player
char name[]; //Name of the people who can sign in
}