15

jpg ファイルを文字列に読み込む必要があります。このファイルをサーバーにアップロードしたいのですが、API がこの写真のデータとして文字列を必要とすることがわかりました。Upload pics to a server using c++ を尋ねた以前の質問の提案に従いました。

int main() {
    ifstream fin("cloud.jpg");
    ofstream fout("test.jpg");//for testing purpose, to see if the string is a right copy
    ostringstream ostrm;

    unsigned char tmp;
    int count = 0;
    while ( fin >> tmp ) {
        ++count;//for testing purpose
        ostrm << tmp;
    }
    string data( ostrm.str() );
    cout << count << endl;//ouput 60! Definitely not the right size
    fout << string;//only 60 bytes
    return 0;
}

なぜ60で止まるのですか?60 で変な文字です。jpg を文字列に読み取るにはどうすればよいですか?

アップデート

ほとんどありますが、提案された方法を使用した後、文字列を出力ファイルに書き直すと、歪んでしまいました。また、ofstream が によってバイナリ モードであることを指定する必要があることがわかりますofstream::binary。終わり!

ところでifstream::binary&の違いは何ですか?ios::binaryの略語はありofstream::binaryますか?

4

3 に答える 3