C程序 - 文件 - FWRITE [英] C Programming - File - fwrite

查看:132
本文介绍了C程序 - 文件 - FWRITE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);

我不知道为什么,但它是不是在所谓的Archivo'二进制文件写入id_doc的价值... ...可能是什么问题呢?
我加id_doc的printf和值为printed..I真的不知道

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

好吧,继承人的全code(更少):

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 :/

推荐答案

三件事情:


  • 请确保您的fopen是成功的。

  • Make sure your fopen is successful.

Archivo=fopen("md.dat", "ab+");
if (Archivo == NULL)
{
     perror("Failed to open file Archivo");
     ...
}


  • 请确保您检查您的fwrite的成功。

  • 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");
         ... 
    }
    


  • 请确保您有FCLOSE正确关闭文件。

  • Make sure you have a fclose to close the file properly.

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


  • 现在,你已经发表您的code的全样本我想我应该问,如果错误检查刚走出去简洁?如果没有,你应该考虑增加它。

    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.

    如果您期望id_doc的价值将在输出文件显示格式,你必须为int转换成字符串(使用的snprintf或类似),并写入字符串到输出文件来代替。

    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.

    这篇关于C程序 - 文件 - FWRITE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆