0

I'm trying to upload a photo to one of our server in C++, the following is an excerpt of my testing code

//in main
ifstream fin("cloud.jpg");
ofstream fout("cloudcpy.jpg");

string data;
while ( fin )
    fin >> data;
fout << data;
fin.close();
fout.close();

return 0;

But the output file is not a copy, much more smaller than the original one. Anything wrong in my code?

Joey.Z
  • 3,450
  • 4
  • 30
  • 59
  • 1
    `while (fin >> data)` would be better anyway (much nicer in general so that you only *use* good data), but why not just use a function that actually copies a file? – chris Jul 11 '13 at 02:45
  • How much *smaller*? What is the original size? – Shoe Jul 11 '13 at 02:46
  • @Jeffrey 10kB VS 1kB(76 bytes actually) – Joey.Z Jul 11 '13 at 02:47
  • Oh, and the files are closed when the streams go out of scope. I don't particularly like putting things in that get done for me. – chris Jul 11 '13 at 02:51
  • @chris There should be one, `read_file()` but seems some libs are missing I can't use it. And you are right! I've maked it work. – Joey.Z Jul 11 '13 at 02:51
  • @zoujyjs, There is Boost Filesystem, which will be standard soon. There are also OS-specific options. I also found this gem in another SO answer: `fout << fin.rdbuf();`, which I can't believe I didn't think of. – chris Jul 11 '13 at 02:54
  • 1
    Zeros will crop your string output. – sje397 Jul 11 '13 at 03:06
  • @sje397 see my comments for the answer of jgmao – Joey.Z Jul 11 '13 at 03:10
  • You also should open fin with the ios::binary flag. – Carlo Wood Nov 20 '16 at 18:22

2 Answers2

1

You need use 3rd party library. Please try libcurl

oldmonk
  • 699
  • 4
  • 10
0

You may not use string as the data type. Since JPEG file is not a textfile. Please try to use primitive type such as unsigned char or unsigned int to do it.

jgmao
  • 127
  • 2
  • 8