我可以使用的fscanf二进制文件在C语言? [英] Can i use fscanf with binary files in the c language?

查看:259
本文介绍了我可以使用的fscanf二进制文件在C语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个考试,并且excercies之一,包括从二进制文件扫描的名称。我用了的fscanf(),但我的教授告诉我,我不能在二进制文件中使用的fscanf。但为什么?我的程序工作得很好,即使我这样做。

So i had an exam, and one of the excercies included to scan names from a binary file. I had used fscanf(), but my prof told me that i cant use fscanf in binary files. but why? my program works just fine even if i do so.

推荐答案

我承认,我没有找到一个方法来解释什么是错的fscanf()和这将很容易理解,但这里的二进制文件是如何产生的二进制文件是由的fscanf()

I admit that I didn't find a way to explain what is wrong with fscanf() and binary files that would be understood easily, but here is an example of how the binary file generated is read erroneously by fscanf()

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Entry
{
    char text[64];
    int  age;
};

int main()
{
    FILE        *file;
    struct Entry entry;

    file = fopen("data.bin", "wb");
    if (file == NULL)
        return -1;

    entry.age = 30;

    memset(entry.text, 0, sizeof(entry.text));
    strcpy(entry.text, "Iharob Al Asimi");

    fwrite(&entry, sizeof(entry), 1, file);

    entry.age = 22;

    memset(entry.text, 0, sizeof(entry.text));
    strcpy(entry.text, "Claudio Urdaneta");

    fwrite(&entry, sizeof(entry), 1, file);

    entry.age = 29;

    memset(entry.text, 0, sizeof(entry.text));
    strcpy(entry.text, "Dayana Orozco");

    fwrite(&entry, sizeof(entry), 1, file);
    fclose(file);

    file = fopen("data.bin", "rb");
    if (file == NULL)
        return -1;
    fprintf(stderr, "with fscanf()\n");
    /* this is intentionally == 1 to prove the point */
    while (fscanf(file, "%63s%d", entry.text, &entry.age) == 1)
        fprintf(stderr, "\t%s, %d\n", entry.text, entry.age);

    rewind(file);

    fprintf(stderr, "with fscanf()\n");
    while (fread(&entry, sizeof(entry), 1, file) == 1)
        fprintf(stderr, "\t%s, %d\n", entry.text, entry.age);

    fclose(file);

    return 0;
}

问题是,的fscanf()将扫描的文字和匹配对你传递给它的说明符。

The problem is that fscanf() will scan for text and match against the specifiers you pass to it.

但二进制文件只是一堆存储与给定结构中的字节,在上面的例子中,我们写的 64 +的sizeof(INT)每页记录的字节,一项目只是文本以便读取字节与的fscanf()按预期工作,它当然在空格字符停止。

But a binary file is just a bunch of bytes stored with a given structure, in the example above we write 64 + sizeof(int) bytes per record, one of the items is just text so reading bytes with fscanf() works as expected and it of course stops at the whitespace character.

但随后数在文件中没有文字重新presantation,因此你不能用的fscanf()

But then the number has no text represantation in the file, and hence you cannot read it with fscanf().

另外,请注意该

while (fscanf(file, "%63s%d", entry.text, &entry.age) == 1)

做的正确的方法是

the correct way of doing it is

while (fscanf(file, "%63s%d", entry.text, &entry.age) == 2)

但什么都不会被打印,因为整数不会被匹配。

but then nothing will be printed because the integer wont be matched.

因此​​,与二进制文件时,你需要

So when working with binary files you need


  • FREAD() / 的fwrite()

  • fread()/fwrite()

而当数据只是文本


  • fprintf中() / 的fscanf()

  • fprintf()/fscanf()

将是不错的。

这篇关于我可以使用的fscanf二进制文件在C语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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