Read a binary file (jpg) to a string using c++

后端 未结 3 1013
被撕碎了的回忆
被撕碎了的回忆 2021-01-02 00:35

I need to read a jpg file to a string. I want to upload this file to our server, I just find out that the API requires a string as the data of this pic. I followed the sugge

3条回答
  •  盖世英雄少女心
    2021-01-02 00:58

    Try opening the file in binary mode:

    ifstream fin("cloud.jpg", std::ios::binary);
    

    At a guess, you were probably trying to read the file on Windows and the 61st character was probably 0x26 -- a control-Z, which (on Windows) will be treated as marking the end of the file.

    As far as how to best do the reading, you end up with a choice between simplicity and speed, as demonstrated in a previous answer.

提交回复
热议问题