C++ 错误:从“const void*”到“void*”的无效转换[-fpermissive]

时间:2021-02-25 15:05:05

标签: c++

我试图从头开始创建一个编译器,在 main.cpp 上我试图读取该文件,但是一旦我编译它就会给我一个错误:

<块引用>

错误:从“const void*”到“void*”的无效转换[-fpremissive]

#include <iostream>

using namespace std;

int main(){
    cout << "Parser 0.1\n" << endl;

    FILE * fh = fopen("C:\\file.bpc", "r"); // file location is correct
    if(!fh){ cerr << "Cannot find file" << endl; }
    fseek(fh, 0, SEEK_END);
    size_t fileSize = ftell(fh);
    fseek(fh, 0, SEEK_SET);
    string fileContents(fileSize, ' ');
    fread(fileContents.data(), 1, fileSize, fh); // HERE THE ERROR OCCURRES

    cout << fileContents << endl;

    return 0;
}

1 个答案:

答案 0 :(得分:0)

这是我用来解决问题的代码,我使用了 fstream 库,而不是以 C 风格读取文件。

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){
    cout << "Parser 0.1\n" << endl;

    string line;
    ifstream file; // Instead of ofstream that creates I file as far as I understand
    file.open("C:\\main1.bpc");
    if(file.is_open()){
        while(getline(file,line)){
            cout << line << endl;
        }
        file.close();
    }

    else cerr << "Unable to open the file" << endl;

    return 0;
}
相关问题