Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   ifstream problem with reading (http://www.go4expert.com/forums/ifstream-reading-t19880/)

exot 25Oct2009 03:55

ifstream problem with reading
hi guys,

maybe i'm complete idiot but i really can't see what's wrong with this:


ifstream fin("data.pdf");
  ofstream fout("bleah.pdf");
  fin.seekg (0, ios::end);
  int length = finc.tellg();
  fin.seekg (0, ios::beg);
  for(int i=0;i<length;i++){

please excuse pointless function of this programm, it's just a model of what is my problem. The length is always correct, but the final file on the output is never complete copy. At some point it changes the eof and fail bit to true and the rest of the data read are just 0xFF bytes. :cryin: :cryin:

SaswatPadhi 25Oct2009 08:23

Re: ifstream problem with reading
Try doing it this way :

Code: C++

ifstream fin("data.pdf");
ofstream fout("bleah.pdf");
char ch;

fin.seekg (0, ios::end);
unsigned int length = fin.tellg();
fin.seekg (0, ios::beg);
while((ch=fin.get())!=EOF)    fout.put(ch);

All times are GMT +5.5. The time now is 19:32.