在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'; } } std::ifstream myfile; open(“example.txt”,std::ios::binary); while(getline(myfile,line)){ 对于(std::size_t i=0;i

在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'; } } std::ifstream myfile; open(“example.txt”,std::ios::binary); while(getline(myfile,line)){ 对于(std::size_t i=0;i,c++,file,stream,ifstream,C++,File,Stream,Ifstream,,通过将一个字符块转换为二进制,可以将文件读取为二进制 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();

,通过将一个字符块转换为二进制,可以将文件读取为二进制

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;
std::streampos大小;
char*memblock;
std::ifstream myfile(“sound.mp3”,std::ios::in | std::ios::binary | std::ios::ate);
//ios::ate将读取器放在文件的末尾
if(file.is_open())
{
size=myfile.tellg();
memblockChar=新字符[大小];
myfile.seekg(0,std::ios::beg);
myfile.read(memblockChar,大小);
myfile.close();

(int i=0;iIT是一个计算机程序。所有文件都是二进制的……在C++标准库中查找“变换”。@ FrRDRIK我如何读取它们?您可以使用<代码> Read <代码>从二进制文件读取到一个内存块,然后将每个字符转换为上面的位。@ Giel.请给我举个例子好吗?