使用C编程读取文件就像二进制文件一样? [英] Read file like a binary file using C programming?

查看:106
本文介绍了使用C编程读取文件就像二进制文件一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hy朋友

我只想知道c编程中的工作文件如何以及想要如何读取文件(可能是图像输入或txt文件)

-当我做的时候(c!= EOF)和c = fgetc(文件)

c编程读取文件的字节就像一个char它意味着一个ASCII

但是我想要读取我的文件只有二进制0或1?

我该怎么做?

-将所有输入文件转换为二进制(所有字节到二进制)并将它们保存到另一个文件二进制文件

你提出什么建议?

thnk you



我尝试过:



我想要讨论的想法

hy friends
I just want how work file in c programming and want idea how I can read file (maybe was image input or txt file )
-when I did when (c!=EOF) and c =fgetc(file)
c programming read the byte of file like a char it means a ASCII
but I want like read my file only binary 0 or 1 ?
how I can will do?
-convert all input file to binary (all byte to binary) and save they to another file binary
what yoou propose?
thnk you

What I have tried:

I want idea just for discussion

推荐答案

没有读过这样的东西,或者你是什么东西'呼叫二进制。您可以获得的最低值是读取单个字节。从那里,你必须解析字节以使用位掩码和逻辑和/或移位运算符来获得所需的位值。
There's no such thing as reading a bit, or what you're calling "binary". The lowest you can get is down to reading a single byte. From there, you have to parse the byte to get the bit values you want using bit masks and the logical and/or shift operators.


文件内容始终是使用8位(字节的二进制) )作为最小单位。表示什么类型的数据取决于文件类型,并且在读取和写入文件时必须由应用程序相应地处理。



fgetc - C ++参考 [ ^ ]函数返回从文件中读取的单字节为 int 。返回 int 而不是字节类型的原因,如 char unsigned char 是错误和文件结尾的额外返回值( EOF )。



所以你不需要任何转换。但您可能需要解释数据以准备使用或只打印出来。



如果您没有关于文件类型的信息,可以打印二进制文件内容:

File content is always binary using 8-bit (byte) as smallest unit. What kind of data is represented depends on the file type and must be handled accordingly by the application when reading and writing the file.

The fgetc - C++ Reference[^] function returns a single byte read from the file as an int. The reason for returning an int instead of a byte type like char or unsigned char is that an additional return value for errors and end of file is required (EOF).

So you don't need any conversion. But you may need to interpret the data to prepare it for usage or just printing it out.

If you have no information about the file type, you can print the binary content:
#include <stdio.h>

int main(int argc, char* argv[])
{
    int c;
    FILE *f;
    if (argc > 1)
    {
        f = fopen(argv[1], "rb");
        if (f)
        {
            do
            {
                c = fgetc(f);
                if (EOF != c)
                    printf("%02X ", c);
            } while (EOF != c);
            fclose(f);
        }
    }
    printf("\n");
    return 0;
}





如果要将每个字节打印为位序列,则必须编写相应的函数(调用此代替上例中的 printf 函数):



If you want to print each byte as bit sequence you have to write a corresponding function (call this instead of the printf function in the above example):

/* Print byte as bit sequence with MSB first */
void print_byte_as_bits(int byte_value)
{
    int i;
    if (byte_value >= 0 && byte_value < 256)
    {
        for (i = 0; i < 8; i++)
        {
            printf("%c ", (byte_value & 0x80) ? '1' : '0');
            byte_value <<= 1;
        }
    }
    else
        printf("[Not a byte: %X]", byte_value);
}


这篇关于使用C编程读取文件就像二进制文件一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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