ifstream不读取文件中存在的`0a`字节
ifstream does not read `0a` byte existing in the file
我想读取文件的前7个字节。这是我文件的数据:
42 4d b6 fc 0a 00 00
我使用此代码来做到这一点:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream r("TestFile.abc", ios::binary);
unsigned char info[7];
for(int i = 0 ; i < 7; i++){
r >> info[i];
}
for(int i = 0 ; i < 7; i++){
std::stringstream ss;
ss << std::hex << (int) info[i]; // int decimal_value
std::string res ( ss.str() );
cout << i << setw(10) << info[i] << setw(10) << res << endl;
}
return 0;;
}
这是输出:
0 B 42
1 M 4d
2 ╢ b6
3 ⁿ fc
4 0
5 0
6 0
Process returned 0 (0x0) execution time : 0.032 s
Press any key to continue.
为什么0a
字节已替换为0
?
>>
跳过空白, 0a
是一个withespace字符。尝试这个
info[i] = r.get();
get
读取单个字节而无需跳过任何内容(除了可能的线转换之端,但是您已经通过使用ios::binary
为此而对此进行了。
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 在UNIX系统中使用DIR查找文件的字节大小
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- std::当在256字节边界上写入整数时,流的奇怪行为
- 当比特(而不是字节)的顺序至关重要时的持久性
- 从文件中读取多个字节,并将它们存储在C++中进行比较
- 如何在文件中查找字节序列
- luaL_dofile在已知良好的字节码上失败,可以使用未编译的版本
- 字节到位运算符重载C++
- 在java中读取c++字节的位字段
- 使用 std::vector::reverse_iterator 将 int 序列化为字节向量?
- 字节真的是最小可寻址单元吗
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 读取文件中所有可能的十六进制 16 字节序列并打印每个序列
- 如何使用 OpenCV 解码在两个 UWP 应用之间发送的图像字节?
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- 如何将原始字节附加到 std::vector?
- ifstream不读取文件中存在的`0a`字节