fwrite问题,无法写入二进制文件!

发布于 2024-10-04 11:36:01 字数 436 浏览 0 评论 0原文

可能的重复:
C 编程 - 文件 - fwrite

包括... >

int main(void)
{

  FILE *Archivo;
  int proba=5;

  Archivo=fopen("md3.dat", "ab+");
  fwrite(&proba, sizeof(int), 1, Archivo);
  fclose(Archivo);
  printf("Bye!\n\n");
}

我只是无法在二进制 fila md3.dat 上打印任何数字,请帮忙!

Possible Duplicate:
C Programming - File - fwrite

include....
>

int main(void)
{

  FILE *Archivo;
  int proba=5;

  Archivo=fopen("md3.dat", "ab+");
  fwrite(&proba, sizeof(int), 1, Archivo);
  fclose(Archivo);
  printf("Bye!\n\n");
}

I just can't print any number on the binary fila md3.dat, please help!

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

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

发布评论

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

评论(2

段念尘 2024-10-11 11:36:01

我认为OP期望fwrite表现得像fprintf并在文件中写入一个ASCII十进制字符串,然后当输出是字节5,0时感到困惑, 0,0(所有不可打印的字符),在编辑器中打开它或使用 cat 将其打印到终端(或 type > 在 DOS 上)。

如果您想写入文本,请使用fprintf

I think OP is expecting fwrite to behave as fprintf and write an ASCII decimal string the the file, then getting confused when the output is the bytes 5,0,0,0 (all non-printable characters) which probably shows up blank when opening it in an editor or using cat to print it to the terminal (or type on DOS).

If you want to write text, use fprintf.

云淡月浅 2024-10-11 11:36:01

试试这个:

Archivo = fopen("md3.sat", "wb+");
fseek(Archivo, 0, SEEK_END);
fwrite(&proba, sizeof(int), 1, Archivo);

这应该在文件末尾写入您的 int 值。另外,您应该检查函数返回值。请记住:你在 C 语言!!

int written;
Archivo = fopen("md3.sat", "wb+");
if (Archivo != NULL) {
    fseek(Archivo, 0, SEEK_END);
    written = fwrite(&proba, sizeof(int), 1, Archivo);
    printf("%d int values written!\n", written);
}

Try this:

Archivo = fopen("md3.sat", "wb+");
fseek(Archivo, 0, SEEK_END);
fwrite(&proba, sizeof(int), 1, Archivo);

This should write your int value at the end of the file. Also, you should check for function return values. Remember: you are in C!!.

int written;
Archivo = fopen("md3.sat", "wb+");
if (Archivo != NULL) {
    fseek(Archivo, 0, SEEK_END);
    written = fwrite(&proba, sizeof(int), 1, Archivo);
    printf("%d int values written!\n", written);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文