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
標識符,並且它運行了,但是它打印的是隨機的東西,甚至沒有接近文件中的內容。有任何想法嗎?結構導致程序崩潰
[更新從comment:]
struct sign_in
{
int password; //The password for each player
char name[]; //Name of the people who can sign in
}
'fscanf(Fpointin,「%s%d」,items [i] .name,items [i] .password);''''''''''''''''''''我猜測密碼是一個字符串。如果是這樣,你需要使用'「%s%s」'作爲格式字符串。 –
發佈'struct sign_in'定義 – red0ct
如何在評論部分發布代碼? – JMBTaylor