从二进制文件读取数据并将其存储在结构中会产生分段错误

发布于 2024-10-06 04:02:33 字数 1688 浏览 0 评论 0原文

我遇到了“分段错误”错误。

我正在从二进制文件读取数据,我试图将其存储在结构中;这是我正在做或“尝试”做的事情的代码:

struct Medico //users are medics
{
  int Id_Doctor; //Id User
  int Estado; //status of the user
  char Nombre[60]; //name of the user
  char Clave_Acceso[20]; //password of the user
  char Especialidad[40]; //especialty of the user
  struct Medico *next;
};

void Cargar_Datos () //load files
{
  FILE *Archivaldo; ///file- Archivo means file
  struct Medico * head = NULL;
  struct Medico * prev, *current;
  char especialida[40], password[20]; ///locals for specialty and password
  char nombre_doc[60]; ///local for name
  int estado_doc, id_doc; // local for status 

  if((Archivaldo=fopen("md.dat", "a+b"))==NULL)
    {
      printf("No se pudo abrir el archivo de Medicos\n");
      exit(1);
    }
  rewind(Archivaldo);
  current = (struct Medico *) malloc (sizeof(struct Medico));

  fread(&id_doc, sizeof(int), 1, Archivaldo);
  fread(nombre_doc, sizeof(char), sizeof(nombre_doc), Archivaldo);
  fread(password, sizeof(char), 20 , Archivaldo);
  fread(especialida, sizeof(char), 40, Archivaldo);
  fread(&estado_doc, sizeof(int), 1, Archivaldo);

  printf("ID: %d\n", id_doc);
  printf("\nDoctor: ");
  puts(nombre_doc);
  printf("\nPassword: ");
  puts(password);
  printf("\nEspecialidad: ");
  puts(especialida);
  printf("\nEstado: ");
  if(estado_doc==1)
    puts("Activo\n");
  else
    puts("Inactivo\n");
  current->Id_Doctor=id_doc;
  strcpy(current->Nombre, nombre_doc); 
  strcpy(current->Clave_Acceso, password); 
  strcpy(current->Especialidad, especialida);
  current->Estado=estado_doc; 
  current=current->next;

  fclose(Archivaldo);

}

I'm having problems with a 'segmentation fault' error.

I'm reading data from a binary file, which I'm trying to store in a structure; here's the code to what I'm doing or 'trying' to do:

struct Medico //users are medics
{
  int Id_Doctor; //Id User
  int Estado; //status of the user
  char Nombre[60]; //name of the user
  char Clave_Acceso[20]; //password of the user
  char Especialidad[40]; //especialty of the user
  struct Medico *next;
};

void Cargar_Datos () //load files
{
  FILE *Archivaldo; ///file- Archivo means file
  struct Medico * head = NULL;
  struct Medico * prev, *current;
  char especialida[40], password[20]; ///locals for specialty and password
  char nombre_doc[60]; ///local for name
  int estado_doc, id_doc; // local for status 

  if((Archivaldo=fopen("md.dat", "a+b"))==NULL)
    {
      printf("No se pudo abrir el archivo de Medicos\n");
      exit(1);
    }
  rewind(Archivaldo);
  current = (struct Medico *) malloc (sizeof(struct Medico));

  fread(&id_doc, sizeof(int), 1, Archivaldo);
  fread(nombre_doc, sizeof(char), sizeof(nombre_doc), Archivaldo);
  fread(password, sizeof(char), 20 , Archivaldo);
  fread(especialida, sizeof(char), 40, Archivaldo);
  fread(&estado_doc, sizeof(int), 1, Archivaldo);

  printf("ID: %d\n", id_doc);
  printf("\nDoctor: ");
  puts(nombre_doc);
  printf("\nPassword: ");
  puts(password);
  printf("\nEspecialidad: ");
  puts(especialida);
  printf("\nEstado: ");
  if(estado_doc==1)
    puts("Activo\n");
  else
    puts("Inactivo\n");
  current->Id_Doctor=id_doc;
  strcpy(current->Nombre, nombre_doc); 
  strcpy(current->Clave_Acceso, password); 
  strcpy(current->Especialidad, especialida);
  current->Estado=estado_doc; 
  current=current->next;

  fclose(Archivaldo);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对你再特殊 2024-10-13 04:02:33

您正在使用 fread 读取 nombre_docespecialida 和所有其他字符串。这很好,但它不会以 '\0' 字符终止字符串。您使用 puts 和 strcpy ,它们期望这些字符串以 nul 终止符结尾。

首先,将字符串的大小增加 1。然后终止字符串:

nombre_doc[60] = '\0';

You are reading nombre_doc, especialida and all other strings with fread. This is fine but it does not terminate the strings with a '\0' character. You use puts and strcpy which are expecting these strings to end with the nul-terminator.

First, increase the size of the strings with 1. After that terminate your strings:

nombre_doc[60] = '\0';
诠释孤独 2024-10-13 04:02:33

调试分段错误的最佳方法是使用 GDB 等调试器或 Valgrind 等内存分析器。

如果没有,通常可以在代码中添加编号的 printf() 语句。当您找到错误之前执行的最后一个 printf() 时,您可以添加更多 printf() 语句并重复测试以缩小范围。

C 程序中分段错误的一些常见原因:

  • 尝试取消引用 NULL 指针。如果所述指针是诸如 malloc() 或 fopen() 之类的函数调用的结果,而在继续之前未检查其输出是否有错误,则经常会发生这种情况。

  • 超出数组或分配块的边缘。未正确以 null 终止的字符串是造成这种情况的常见原因。如果打印字符串在屏幕上产生垃圾,这可能就是原因。

  • 尝试使用已通过 free() 释放的内存块。

The best way to debug a segmentation fault is to use a debugger such as GDB or a memory analyser such as Valgrind.

If one is not available, it usually helps to add numbered printf() statements in the code. When you find the last printf() that was executed before the error, you can add more printf() statements and repeat your tests to narrow it down.

A few common causes of a segmentation fault in C program:

  • Trying to dereference a NULL pointer. That often happens if said pointer is the result of a function call such as malloc() or fopen(), whose output was not checked for errors before proceeding.

  • Going beyond the edges of an array or allocated block. Strings that are not null-terminated properly are a common cause of this. If printing a string produces garbage in the screen, this could be the cause.

  • Trying to use a memory block that has already been freed with free().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文