读取C 中的二进制文件的任何文件
Reading any file as binary in C++?
我如何读取C 中的二进制文件类型?到目前为止,我能够使用std::bitset
在二进制中读取.txt文件,例如:
std::ifstream myfile;
myfile.open("example.txt", std::ios::binary);
while (getline (myfile, line) ) {
for (std::size_t i = 0; i<line.size(); ++i) {
std::bitset<8> a = std::bitset<8>(line[i]); //convert every character to binary, save it in a
std::cout<<((char)std::bitset<8>(a).to_ulong())<<'n';
}
}
在第一行,我如何将sound.mp3
或word.docx
之类的文件读取为二进制文件?我了解它们实际上只是二进制文件,但是我该如何阅读它们?
谢谢!
通过从字符的存储器块铸造到二进制文件,您可以将文件读为二进制文件。
std::streampos size;
char * memblock;
std::ifstream myfile ("sound.mp3", std::ios::in|std::ios::binary|std::ios::ate);
//ios::ate puts the reader at the end of the file
if (file.is_open())
{
size = myfile.tellg();
memblockChar = new char [size];
myfile.seekg (0, std::ios::beg);
myfile.read (memblockChar, size);
myfile.close();
for (int i = 0; i<size; i++) {
std::cout << (((std::bitset<8>)memblockChar[i]).to_ulong()) << 'n';
}
delete[] memblockChar;
}
else std::cout<<"Unable to open file"<<std::endl;
这可以在主要方法或其他任何地方使用。
相关文章:
- Assimp - 如何使用任何文件格式导入带有纹理的网格?
- C++ - 将任何文件的字节读取到无符号字符数组中
- 如何检查特定文件夹中是否存在任何文件?
- VS Code认为任何文件的第一行都是#include错误
- 如何检查目录中是否打开了任何文件?
- 读取C 中的二进制文件的任何文件
- 如何打开并显示C 中任何文件扩展名的文件
- 如何使用流显示任何文件中的特殊和隐藏字符
- Windows 7:MFC ActiveX 控件不会在任何文件夹中创建文件
- 如何将任何文件安全地读取为字符串
- 如何在任何文件中包含带有cuda常量的头文件
- 可以在项目的任何文件中使用的C++变量
- 提升 BCP 不输出任何文件?
- 将任何文件视为加密/解密的比特流
- 将任何文件包含在主可执行文件中
- std::getline() 访问冲突,以前可以工作,但现在无法从任何文件中读取
- QPixmap::save() 返回成功,但不生成任何文件
- 逐字节读取任何文件(不是.bin文件)
- 无法在运行时打开 Code::Blocks 应用程序中的任何文件
- visual C++ifstream不会打开任何文件