C 编程 - 文件 - fwrite
我有一个关于编程和文件的问题。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您正在传递一个指向四字节整数的指针,但只写入一个字节(错误字节)!
解决方案:将 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".
您之前已将字符串
"\n\n"
和""
写入文件Archivo
,这似乎表明它根本不是二进制文件,而是 XML 样式的文件。在这种情况下,您几乎肯定想要的是:
You have previously written the strings
"\n<md>\n"
and"<id_doctor> "
to the fileArchivo
, 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:
尝试添加 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 anelse
since there is no other thing it can be but'\0'
三件事:
确保您的 fopen 成功。
确保您正在检查 fwrite 是否成功。
确保您有 fclose 来正确关闭文件。
既然您已经发布了代码的完整示例,我想我应该问是否为了简洁而省略了错误检查?如果没有,您应该考虑添加它。
如果您希望 id_doc 的值在输出文件中采用显示格式,则必须将 int 转换为字符串(使用 snprintf 或类似函数)并将该字符串写入输出文件。
Three things:
Make sure your fopen is successful.
Make sure you are checking the success of your fwrite's.
Make sure you have a fclose to close the file properly.
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.
如果您将 id_doc 定义为字符以外的任何内容,它会将 \0 写入文件。
更干净的是:
If you defined id_doc as anything other than a char it will write \0 to the file.
Much cleaner would be:
如果您的第一个
current
是Id_Doctor
,那么您就会陷入无限循环。如果在最后一个
current
之后不存在不是Id_Doctor
的current
,则会出现非法指针取消引用。对于您的问题:
尝试flush()系列。
If your first
current
is anId_Doctor
you have an endless loop.If there is no
current
after your lastcurrent
that is not anId_Doctor
, you get an illegal pointer derefenciation.For your Problem:
try the flush() family.