我正在尝试将文件中的名称和密码读入 c 中的结构,但显然我的代码没有按预期工作。有没有人可以帮助我找出下面附加代码的问题?非常感谢!(基本上文件有几个名字和密码,我想把它们读入一个结构accounts[]`)
#include <stdio.h>
#include <stdlib.h>
struct account {
    char *id; 
    char *password;
};
static struct account accounts[10];
void read_file(struct account accounts[])
{
    FILE *fp;
    int i=0;   // count how many lines are in the file
    int c;
    fp=fopen("name_pass.txt", "r");
    while(!feof(fp)) {
        c=fgetc(fp);
        if(c=='\n')
            ++i;
    }
    int j=0;
    // read each line and put into accounts
    while(j!=i-1) {
        fscanf(fp, "%s %s", accounts[j].id, accounts[j].password);
        ++j;
    }
}
int main()
{
    read_file(accounts);
    // check if it works or not
    printf("%s, %s, %s, %s\n",
        accounts[0].id, accounts[0].password,
        accounts[1].id, accounts[1].password);
    return 0;
}
name_pass.txt 文件是一个像这样的简单文件(名称+密码):
你好 1234
大声笑 123
世界123