简体   繁体   English

从每个字节C ++的文件字节读取

[英]Reading from a file byte per byte C++

Im trying to write a program in C++ that will take 2 files and compare them byte by byte. 我试图用C ++编写一个程序,它将采用2个文件并逐字节地比较它们。

I was looking at the following post 我在看以下帖子

Reading binary istream byte by byte 逐字节读取二进制istream

Im not really sure about parts of this. 我不确定这部分内容。 When using get(char& c) it reads in a char and stores it in c. 当使用get(char&c)时,它读入一个char并将其存储在c中。 Is this storing as, say 0x0D, or is it storing the actual char value "c" (or whatever)? 这是存储为,例如0x0D,还是存储实际的char值“c”(或其他)?

If i wish to use this method to compare two files byte by byte would i just use get(char& c) on both then compare the chars that were got, or do i need to cast to byte? 如果我希望使用这种方法逐字节地比较两个文件,我会在两者上使用get(char&c)然后比较得到的字符,还是我需要转换为字节?

(I figured starting a new post would be better since the original is quite an old one) (我认为开始一个新帖子会更好,因为原来是一个很旧的帖子)

char s are nothing but a "special type of storage" (excuse the expression) for integers, in memory there is no difference between 'A' and the decimal value 65 (ASCII assumed). char只是整数的“特殊类型的存储”(借用表达式),在内存中, 'A'和十进制值65 (假定的ASCII)之间没有区别。

c will in other words contain the read byte from the file. 换句话说, c将包含文件中的读取字节。


To answer your added question; 回答你提出的问题; no, there is no cast required doing c1 == c2 will be just fine. 不,没有演员要求做c1 == c2就好了。


  char c1 = 'A', c2 = 97, c3 = 0x42; 

  std::cout <<  c1  << "  " <<    c2 << "  " <<    c3 << std::endl;
  std::cout << +c1  << " "  <<   +c2 << " "  <<   +c3 << std::endl;

/* Writing +c1 in the above will cast c1 to an int , it's is the same thing as writing (int)c1 or the more correct (c++ish) static_cast<int> (c1) . / *上面的 写入+c1会将c1转换为int ,它与write (int)c1或更正确(c ++ ish) static_cast<int> (c1) */ * /

output: 输出:

A  a  B
65 97 66

Ehm, 埃姆,

a char contains 1 Byte The interpretation of that value is indeed depending on you, the programmer. 一个char包含1个字节该值的解释确实取决于你,程序员。

If you print that byte in the cout stream it is interpreted via ASCII Code and therefor if your char was 0x63 then it will print 'c' on the screen. 如果你在cout流中打印该字节,它将通过ASCII代码解释,因此如果你的字符是0x63,那么它将在屏幕上打印'c'。

If you just use the value you can use it as you like.. 如果您只是使用该值,您可以随意使用它..

char c = 0x63;
c++;
// c is now: 0x64

Note that you can also input decimals 请注意,您还可以输入小数

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

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