简体   繁体   English

如何复制文件的二进制数据

[英]how to copy binary data of a file

Basically, I am trying to read binary data of a file by using fread() and print it on screen using printf(), now, the problem is that when it prints it out, it actually don't show it as binary 1 and 0 but printing symbols and stuff which I don't know what they are. 基本上,我试图通过使用fread()读取文件的二进制数据并使用printf()在屏幕上打印它,现在的问题是,当它打印出来时,实际上并没有将其显示为二进制1,并且0,但打印不知道它们是什么的符号和内容。

This is how I am doing it: 这就是我的做法:

#include <stdio.h>
#include <windows.h>


int main(){
    size_t  sizeForB, sizeForT;
    char    ForBinary[BUFSIZ], ForText[BUFSIZ];

    char    RFB []      = "C:\\users\\(Unknown)\\Desktop\\hi.mp4"   ;       // Step 1

    FILE    *ReadBFrom  = fopen(RFB , "rb" );

    if(ReadBFrom == NULL){
        printf("Following File were Not found: %s", RFB);
        return -1;
    } else {
        printf("Following File were found: %s\n", RFB);                              // Step 2
        while(sizeForB = fread(ForBinary, 1, BUFSIZ, ReadBFrom)){                      // Step 1
           printf("%s", ForBinary);
        }
        fclose(ReadBFrom);

    }

    return 0;
}

I would really appreciate if someone could help me out to read the actual binary data of a file as binary (0,1). 如果有人可以帮助我将文件的实际二进制数据读取为二进制(0,1),我将不胜感激。

 while(sizeForB = fread(ForBinary, 1, BUFSIZ, ReadBFrom)){             
           printf("%s", ForBinary);  }

This is wrong on many levels. 这在许多层面上都是错误的。 First of all you said it is binary file - which means there might not be text in it in the first place, and you are using %s format specifier which is used to print null terminated strings. 首先,您说它是二进制文件-这意味着它首先可能没有文本,并且您使用的是%s格式说明符,该说明符用于打印以null结尾的字符串。 Again since this is binary file, and there might not be text in it in the first place, %s is the wrong format specifier to use. 同样,由于这是二进制文件,并且一开始可能没有文本,因此%s是错误的格式说明符。 And even if there was text inside this file, you are not sure that fread would read a "complete" null terminated string that you could pass to printf with format specifier %s . 而且即使该文件中包含文本,您也不确定fread是否会读取“完整”的以null结尾的字符串,您可以使用格式说明符%s将其传递给printf

What you may want to do is, read each byte form a file, convert it to a binary representation (google how to convert integer to binary string say, eg, here ), and print binary representation for each that byte. 您可能想要做的是,从文件中读取每个字节,将其转换为二进制表示形式(谷歌如何将整数转换为二进制字符串,例如here ),然后为每个字节打印二进制表示形式。

Basically pseudocode: 基本上是伪代码:

foreach (byte b in FileContents)
{
  string s = convertToBinary(b);
  println(s);
}

How to view files in binary in the terminal? 如何在终端中查看二进制文件?

Either 要么

"hexdump -C yourfile.bin" perhaps, unless you want to edit it of course. 也许“ hexdump -C yourfile.bin”,除非您当然要对其进行编辑。 Most linux distros have hexdump by default (but obviously not all). 大多数Linux发行版默认都有hexdump(但显然不是全部)。

or 要么

xxd -b file xxd -b文件

To simply read a file and print it in binary (ones and zeros), read it one char at a time. 要简单地读取文件并以二进制(一和零)打印,请一次读取一个char Then for each bit, print a '0' or '1' . 然后为每一位打印一个'0''1' Can print Most or Least significant bit first. 可以先打印最高或最低有效位。 Suggest MSb. 建议MSb。

if (ReadBFrom) {
  int ch;
  while ((ch = fgetc(ReadBFrom)) != EOF) {
    unsigned mask = 1u << (CHAR_BIT - 1);  // CHAR_BIT is typically 8
    while (mask) {
       putchar(mask & ch ? '1' : '0');
       mask >>= 1;
    }
  }
  fclose(ReadBFrom);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM