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

后端 未结 3 1012
被撕碎了的回忆
被撕碎了的回忆 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 01:00

    Open the file in binary mode, otherwise it will have funny behavior, and it will handle certain non-text characters in inappropriate ways, at least on Windows.

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

    Also, instead of a while loop, you can just read the whole file in one shot:

    ostrm << fin.rdbuf();
    

提交回复
热议问题