イメージをchar配列にコピーしようとしています。ドキュメントの例のようですが、正しく動作しません。下のコードで何が間違っていますか?なぜこのコードはイメージをchar配列にコピーしませんか?
char* getImage(char* path){
ifstream image;
image.open(path, ifstream::binary);
image.seekg (0, ios::end);
int n = image.tellg();
printf("n = %d\n", n);
image.seekg (0, ios::beg);
char* res = new char[n];
image.read(res, n);
if (image)
std::cout << "all characters read successfully." << endl;
printf("length = %d", strlen(res));
cout<<res<<"\n\n";
image.close();
return res;
}
myプログラムの出力。
n = 6942
all characters read successfully.
length = 4
あなたのイメージは、おそらくヌルで終了する文字列ではありません。 std :: strlenはちょうど最初のゼロバイトでチョークし、 'std :: cout'は何も意味のあるものを表示することはできません。 – Quentin
プログラムの出力を指定しました。良いように思える。あなたはそれが他に何かを出力すると思いましたか? – anatolyg
イメージの長さは 'strlen(res)'ではなく 'n'です。それがあなたの過ちです。 – PaulMcKenzie