导致程序崩溃的结构

Structure causing program to crash

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 标识符,它 运行 但它打印了 运行dom 内容,这些内容甚至没有关闭文件中的内容。有什么想法吗?


[更新自 :]

struct sign_in
{ 
  int password; //The password for each player 
  char name[]; //Name of the people who can sign in
}

如果能看到 sign_in 结构是什么样的,那将非常有帮助。但是,快速浏览一下代码,一个明显的错误是扫描密码的方式。

fscanf(Fpointin,"%s %d",items[i].name,items[i].password);

这一行应该是:

fscanf(Fpointin,"%s %d",items[i].name, &items[i].password);

需要传入变量items[i].password的地址,fscanf才能在该地址指向的内存位置存储一个值

希望对您有所帮助。

可以是char *类型,你可能没有给它分配space。那肯定会崩溃

中的char name[];
struct sign_in
{
  ...
  char name[]; 

类型不完整。它没有分配内存。

使用

#define NAME_LEN_MAX 42

...

struct sign_in
{
  ...
  char name[NAME_LEN_MAX + 1];

例如,像这样调整扫描:

fscanf(Fpointin, "%42s %d", items[i].name, &items[i].password);

首先,你不应该在你的结构中使用char name[]。因为数组应该在你使用它之前清除它的内存大小。

因此,您可以将 char name[] 更改为:

  1. char *name :你必须为它分配一个内存大小。检查下面我的代码。
  2. char name[NAME_SIZE]

Second,如果你想改变另一个函数中的值,你应该将它的内存地址传递给它。例如:

示例 1: 如果只传递值而不传递地址

void foo(int in)
{
    in = 5;
}

int main(int argc, char const *argv[])
{
    /* code */
    int a = 10;
    foo(a);
    printf("after foo, a: %d\n", a);
    return 0;
}

输出: after foo, a: 10

例2:如果传内存地址。

void foo(int *in)
{
    *in = 5;
}

int main(int argc, char const *argv[])
{
    /* code */
    int a = 10;
    foo(&a);
    printf("after foo, a: %d\n", a);
    return 0;
}

输出: after foo, a: 5

因此,您必须将 items[i].password 的内存地址传递给 fscanf,例如:
fscanf(Fpointin,"%s %d",items[i].name, &items[i].password);

因此,您的代码应如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct sign_in {
    int password;//The password for each player
    char *name;//Name of the people who can sign in
};

void init_myStruct(struct sign_in *s_in) {
    s_in->name = calloc(1, sizeof(char)*1024);
    return;
}

void destroy_myStruct (struct sign_in *s_in) {
    free(s_in->name);
    s_in->name = NULL;
    return;
}

int get_name()
{
    int i = 0;
    FILE *Fpointin = NULL;
    struct sign_in items[6];//array of six structure variables

    for ( i = 0; i < 6; i++)
        init_myStruct(&items[i]);

    Fpointin =fopen("namepass.txt","r");
    if (Fpointin == NULL) {
        printf ("File does not exist.\n");
        goto end_of_use;
    }
    else
    {
        printf("Here is the sign_in structure\n");//print the entirety of the sign_in structure
        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("name: %s\ncode: %d\n\n", items[i].name, items[i].password);
        }
    }
    fclose(Fpointin);

end_of_use:
    for ( i = 0; i < 6; i++)
        destroy_myStruct(&items[i]);
    return;
}

int main(int argc, char const *argv[])
{
    get_name();
    return 0;
}