C++: Everytime I read in by fstream I got 1 extra character at the end

前端 未结 2 508
被撕碎了的回忆
被撕碎了的回忆 2021-02-08 20:52

Everytime I read in by fstream I got 1 extra character at the end, How can I avoid this?

EDIT:

ifstream readfile(inputFile);
ofstream writefile(outputFil         


        
2条回答
  •  终归单人心
    2021-02-08 21:33

    Based on the code, it appears what you're trying to do is copy the contents of one file to another?

    If so, I'd try something like this:

    ifstream fin(inputFile, ios::binary);
    
    fin.seekg(0, ios::end);
    long fileSize = fin.tellg();
    fin.seekg(0, ios::beg);
    
    char *pBuff = new char[fileSize];
    fin.read(pBuff, fileSize);
    fin.close();
    
    ofstream fout(outputFile, ios::binary)
    fout.write(pBuff, fileSize);
    fout.close;
    delete [] pBuff;
    

提交回复
热议问题