[英]C Programming - File - 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);
I dont know why but it aint writing the value of id_doc on the binary file called 'Archivo'...what could be the problem? 我不知道为什么,但是它没有在名为“ Archivo”的二进制文件中写入id_doc的值...可能是什么问题? I added a printf of id_doc and the value was printed..I really dont know
我添加了一个id_doc的printf,并且该值已打印..我真的不知道
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 :/ 我在这里快死了,请帮助:/
Try adding fflush(Archivo);
尝试添加
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'
另外,该语句:
if(current->Id_Doctor=='\\0')
确实应该是else
因为除了'\\0'
它别无其他
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. 确保您正在检查fwrite的成功。
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. 如果期望id_doc的值在输出文件中为显示格式,则必须将int转换为字符串(使用snprintf或类似方法),然后将字符串写入输出文件。
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. 如果您将id_doc定义为除char之外的任何其他字符,它将在文件中写入\\ 0。
Much cleaner would be: 更干净的是:
fwrite(&id_doc, sizeof(id_doc), 1, Archivo);
If your first current
is an Id_Doctor
you have an endless loop. 如果您的第一个
current
是一个Id_Doctor
那么您将有一个无限循环。
If there is no current
after your last current
that is not an Id_Doctor
, you get an illegal pointer derefenciation. 如果在上一个
current
之后没有一个不是Id_Doctor
current
,则会得到非法的指针取消折旧。
For your Problem: try the flush() family. 针对您的问题:尝试使用flush()系列。
You're passing a pointer to a FOUR-BYTE INT, but only writing ONE BYTE (the wrong byte)! 您正在传递一个指向四字节INT的指针,但仅写入一个字节( 错误的字节)!
Solution: declare id_doc as "char", not "int". 解决方案:将id_doc声明为“ char”,而不是“ int”。
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. 您先前已将字符串
"\\n<md>\\n"
和"<id_doctor> "
写入文件Archivo
,这似乎表明它根本不是二进制文件,而是XML样式的文件。
In this case, what you almost certainly want is: 在这种情况下,您几乎可以肯定想要的是:
fprintf(Archivo, "%d", id_doc);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.