c - 在project.exe : 0xC0000005: Access violation writing location 0x007110E8 中的0x5409B211 (ucrtbased.dll)处抛出异常

标签 c

我正在尝试从文件中填充 struct hashtag 数组的 hash_name

结构主题标签是

typedef struct{
    char hash_name[300];
    long hash_freq;
    ID_liste users;
}hashtag;

我的功能是

 void load_hashtag(long ID,int* taille,hashtag *local)
 {
  int i=-1;
  char filename[100];
  sprintf(filename, "data\\fn\\%d.featnames", ID);
  long a;
    FILE * g=fopen(filename,"r");
    do{
        if (i >= 0)
        {
            local = (hashtag*)realloc(local, sizeof(hashtag));
            printf("realloc %d\n", i);
        }
        i++;
        fscanf(g,"%ld",&a);  //a numbre i don't want
        fseek(g, 2, SEEK_CUR); //tow characters i don' want
        fscanf(g, "%s", local[i].hash_name);
    }while(!feof(g));
   fclose(g);
  *taille = i;
} 

主要是

int main()
{
    int i,j;
    hashtag* local = (hashtag*)malloc(sizeof(hashtag));
    int local_taille;
    long ID_user;
    FILE* user;
    user = fopen("User.txt", "r");
    if (user == NULL)
    {
        printf("Error opening file\n");
        return 0;
    }
    fscanf(user, "%ld", &ID_user);
    load_hashtag(ID_user,&local_taille,local);
    fclose(user);
    system("Pause");
    return 0;
}

从 1300 到 50 fscanf 后,程序停止并出现异常抛出窗口

请帮忙

最佳答案

这一行是你的问题:

local = (hashtag*)realloc(local, sizeof(hashtag));

您正在将 local 重新分配为之前的大小。你应该realloc更大尺寸,而不是相同尺寸。

关于c - 在project.exe : 0xC0000005: Access violation writing location 0x007110E8 中的0x5409B211 (ucrtbased.dll)处抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37506182/

相关文章:

c - if (!boolvar) { ... 在 1 个 asm 指令中可以做吗?

c - if 语句中的 MPI_Bcast?

c - 这些 LAPACK 计划有何不同之处?一个编译,另一个不编译

c - IOCTL 调用不适用于驱动程序

c - fread 为什么以二进制模式读取以 ff fe 开头的文件返回 1

c - 用 C 语言向另一个程序发送文本命令

c - 为什么在 short/int/long 中多了一个整数类型?

c - 使用 "scanf()"的返回值来检查文件结尾

c++ - 将 Int32 NAN 转换为 float

c - 主机和客户端无法在我的聊天程序中使用 select() 进行通信