using fstream to read every character including spaces and newline

前端 未结 13 1574
一个人的身影
一个人的身影 2020-12-07 22:47

I wanted to use fstream to read a txt file.

I am using inFile >> characterToConvert, but the problem is that this omits any spaces an

13条回答
  •  被撕碎了的回忆
    2020-12-07 23:39

    For encryption, you're better off opening your file in binary mode. Use something like this to put the bytes of a file into a vector:

    std::ifstream ifs("foobar.txt", std::ios::binary);
    
    ifs.seekg(0, std::ios::end);
    std::ifstream::pos_type filesize = ifs.tellg();
    ifs.seekg(0, std::ios::beg);
    
    std::vector bytes(filesize);
    
    ifs.read(&bytes[0], filesize);
    

    Edit: fixed a subtle bug as per the comments.

提交回复
热议问题