将二进制文件作为 char * 缓冲区读取时,它会继续将其作为二进制而不是字符读取

When reading in a binary file as a char * buffer, it keep reading it as binary instead of as characters

void reader(string fileName){
     ifstream ifile; 
     ifile.open(fileName, ios::binary | ios::in);
     ifile.seekg (0, ifile.end);
     int length = ifile.tellg();
     ifile.seekg (0, ifile.beg);
     char * buffer = new char [length];
while(ifile.good()){

    // read data as a block:
    ifile.read (buffer,4);
    cout << buffer <<endl;
    //ifile.read((char *)&inputName, sizeof(int));
    //cout << inputName;

}
ifile.close();

它给出的输出如下所示:

▒d▒▒root.d[$Apd▒▒
endXroot.d

当我希望它看起来更像

root.d
endXroot.d

数据作为 root 输入到文件中。d/0endXroot.d 不知道有没有帮助。

ifstream ifile; 
    ifile.open(fileName, ios::binary | ios::in |ios::ate);
    if(ifile.good()){
        char * buffer;
        long size;
        ifstream file (fileName, ios::in|ios::binary|ios::ate);
        size = file.tellg();
        file.seekg (0, ios::beg);
        buffer = new char [size];
        file.read (buffer, size);
    }
 }

我通过让它获取大小然后直接读取它来修复它。我还解决了有关如何为二进制文件编写写入的潜在问题。