C 编程 - 文件 - fwrite

发布于 2024-10-04 16:27:28 字数 1628 浏览 1 评论 0原文

我有一个关于编程和文件的问题。

while(current!=NULL)
{
  if(current->Id_Doctor!='\0')
  {
    current=current->next;
    id_doc=(current->Id_Doctor);
  }
  if(current->Id_Doctor=='\0')
  {
    id_doc=id_doc+1;
    printf("%d", id_doc);
    break;
  }
}
fwrite(&id_doc, sizeof(char), 1, Archivo);

我不知道为什么,但它没有在名为“Archivo”的二进制文件上写入 id_doc 的值...可能是什么问题? 我添加了 id_doc 的 printf 并打印了该值..我真的不知道

好吧,这是完整的代码(更多-更少):

struct Medico
{
  int Id_Doctor;
  int Estado;
  char Nombre[60];
  char Clave_Acceso[20];
  char Especialidad[40]; 
  struct Medico *next;
};
void Dar_Alta_Med (int estado);
void MenuPrincipal(char enta);
int main(void)
{
  char enta;
  MenuPrincipal(enta);
}
void Dar_Alta_Med(int estado)
{
  struct Medico * head = NULL;
  struct Medico * prev, *current;
  char nombre_doc[60], especialida[40], password[20];
  int id_doc=0, estado_doc=1;
  FILE *Archivo;
 const char *md1="\n<md>\n";
  const char *id_doc1="<id_doctor> ";
 Archivo=fopen("md.dat", "ab+");
  fwrite(md1, 1, strlen(md1), Archivo);
  fwrite(id_doc1, 1, strlen(id_doc1), Archivo);
  current = (struct Medico *) malloc (sizeof(struct Medico));
  current->Id_Doctor=id_doc; 
  while(current!=NULL)
    {
      if(current->Id_Doctor!='\0')
    {
      current=current->next;
      id_doc=(current->Id_Doctor);
    }
      else
    {
      id_doc=id_doc+1;
      printf("%d", id_doc);
      break;
    }
    }
  fwrite(&id_doc, sizeof(id_doc), 1, Archivo);
  printf("Ingresa el nombre del Doctor a dar de alta: ");
  fclose(Archivo);
}

我在这里快死了,请帮忙:/

I've got a question regarding programming and files.

while(current!=NULL)
{
  if(current->Id_Doctor!='\0')
  {
    current=current->next;
    id_doc=(current->Id_Doctor);
  }
  if(current->Id_Doctor=='\0')
  {
    id_doc=id_doc+1;
    printf("%d", id_doc);
    break;
  }
}
fwrite(&id_doc, sizeof(char), 1, Archivo);

I dont know why but it aint writing the value of id_doc on the binary file called 'Archivo'...what could be the problem?
I added a printf of id_doc and the value was printed..I really dont know

Ok, heres the full code(more-less):

struct Medico
{
  int Id_Doctor;
  int Estado;
  char Nombre[60];
  char Clave_Acceso[20];
  char Especialidad[40]; 
  struct Medico *next;
};
void Dar_Alta_Med (int estado);
void MenuPrincipal(char enta);
int main(void)
{
  char enta;
  MenuPrincipal(enta);
}
void Dar_Alta_Med(int estado)
{
  struct Medico * head = NULL;
  struct Medico * prev, *current;
  char nombre_doc[60], especialida[40], password[20];
  int id_doc=0, estado_doc=1;
  FILE *Archivo;
 const char *md1="\n<md>\n";
  const char *id_doc1="<id_doctor> ";
 Archivo=fopen("md.dat", "ab+");
  fwrite(md1, 1, strlen(md1), Archivo);
  fwrite(id_doc1, 1, strlen(id_doc1), Archivo);
  current = (struct Medico *) malloc (sizeof(struct Medico));
  current->Id_Doctor=id_doc; 
  while(current!=NULL)
    {
      if(current->Id_Doctor!='\0')
    {
      current=current->next;
      id_doc=(current->Id_Doctor);
    }
      else
    {
      id_doc=id_doc+1;
      printf("%d", id_doc);
      break;
    }
    }
  fwrite(&id_doc, sizeof(id_doc), 1, Archivo);
  printf("Ingresa el nombre del Doctor a dar de alta: ");
  fclose(Archivo);
}

Im dying here, please help :/

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

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

发布评论

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

评论(6

静赏你的温柔 2024-10-11 16:27:29

您正在传递一个指向四字节整数的指针,但只写入一个字节(错误字节)!

解决方案:将 id_doc 声明为“char”,而不是“int”。

You're passing a pointer to a FOUR-BYTE INT, but only writing ONE BYTE (the wrong byte)!

Solution: declare id_doc as "char", not "int".

久光 2024-10-11 16:27:29

您之前已将字符串 "\n\n""" 写入文件 Archivo,这似乎表明它根本不是二进制文件,而是 XML 样式的文件。

在这种情况下,您几乎肯定想要的是:

fprintf(Archivo, "%d", id_doc);

You have previously written the strings "\n<md>\n" and"<id_doctor> " to the file Archivo, which seems to indicate that it is not a binary file at all, but rather an XML-style file.

In this case, what you almost certainly want is:

fprintf(Archivo, "%d", id_doc);
没企图 2024-10-11 16:27:28

尝试添加 fflush(Archivo); 来强制写入所有缓冲数据。

另外,这个语句: if(current->Id_Doctor=='\0') 确实应该是一个 else 因为除了 <代码>'\0'

Try adding fflush(Archivo); to force a write of all buffered data.

Also, this statement: if(current->Id_Doctor=='\0') really ought to be an else since there is no other thing it can be but '\0'

如日中天 2024-10-11 16:27:28

三件事:

  • 确保您的 fopen 成功。

    Archivo=fopen("md.dat", "ab+");
    if (档案== NULL)
    {
         perror("无法打开文件 Archivo");
         ...
    }
    
  • 确保您正在检查 fwrite 是否成功。

    if (fwrite(&id_doc, sizeof(id_doc), 1, Archivo) < 1)    
    {    
         perror("无法写入文件 Archivo");
         ... 
    }
    
  • 确保您有 fclose 来正确关闭文件。

    if (fclose(Archivo) != 0)    
    {    
        perror("无法关闭文件 Archivo");     
        ...
    }
    

既然您已经发布了代码的完整示例,我想我应该问是否为了简洁而省略了错误检查?如果没有,您应该考虑添加它。

如果您希望 id_doc 的值在输出文件中采用显示格式,则必须将 int 转换为字符串(使用 snprintf 或类似函数)并将该字符串写入输出文件。

Three things:

  • Make sure your fopen is successful.

    Archivo=fopen("md.dat", "ab+");
    if (Archivo == NULL)
    {
         perror("Failed to open file Archivo");
         ...
    }
    
  • Make sure you are checking the success of your fwrite's.

    if (fwrite(&id_doc, sizeof(id_doc), 1, Archivo) < 1)    
    {    
         perror("Failed to write to file Archivo");
         ... 
    }
    
  • Make sure you have a fclose to close the file properly.

    if (fclose(Archivo) != 0)    
    {    
        perror("Failed to close file Archivo");     
        ...
    }
    

Now that you've post a full sample of your code I guess I should ask if error checking is just left out for brevity? If not, you should think about adding it.

If you're expecting the value of id_doc to be in display format in the output file you'll have to convert the int to a string (using snprintf or similar) and write the string to the output file instead.

朦胧时间 2024-10-11 16:27:28
fwrite(&id_doc, sizeof(char), 1, Archivo); 

如果您将 id_doc 定义为字符以外的任何内容,它会将 \0 写入文件。

更干净的是:

fwrite(&id_doc, sizeof(id_doc), 1, Archivo); 
fwrite(&id_doc, sizeof(char), 1, Archivo); 

If you defined id_doc as anything other than a char it will write \0 to the file.

Much cleaner would be:

fwrite(&id_doc, sizeof(id_doc), 1, Archivo); 
泪意 2024-10-11 16:27:28

如果您的第一个currentId_Doctor,那么您就会陷入无限循环。

如果在最后一个 current 之后不存在不是 Id_Doctorcurrent,则会出现非法指针取消引用。

对于您的问题:
尝试flush()系列。

If your first current is an Id_Doctor you have an endless loop.

If there is no current after your last current that is not an Id_Doctor, you get an illegal pointer derefenciation.

For your Problem:
try the flush() family.

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