tags:

views:

509

answers:

2

Hi,

I am trying to read data from a file into an HBufC8 variable and then write the same data into another file. Here is my testcode; the inner loop of the read repeats 4 times to read 5000 chars in each iteration. However, when I try to write the data back into a file, the file "testfile.txt" is empty. Will play a little bit more around with it but help would be appreciated as always ;) Thanks

  HBufC8* iFileBuffer = HBufC8::NewL(20000); 
     TPtr8 ptr(iFileBuffer->Des());

  fp.Seek(ESeekStart,pos);

     Err = fp.Read(ptr);

  while(!Err && ptr.Length()>0)
  {
  Err = fp.Read(ptr);
  Printf(_L("Data read: %d \n\n"), ptr.Length());   
  } 

  fp.Create(iFileServer,_L("C:\\testfile.txt"), EFileWrite);

     fp.Seek(ESeekEnd,pos);   

  fp.Write(*iFileBuffer);
  fp.Close();

  delete(iFileBuffer);
A: 

The first thing I did when playing around with Symbian was to ditch their insane IO classes and use normal C IO:

for instance, this writes a png file from a symbian memorybuffer

 #include <stdio.h>
 #include <string.h>

 fp = fopen("c:\\data\\others\\pngfile.png","wb");
 if(fp)
 {
      fwrite((char*)httpInputBufDesc->Ptr(), httpInputBufDesc->Length(), 1, fp);
      fclose(fp);
      fflush(fp);
 }
 else
 {
      LOG("Failed to write to disk\n");
 }

as a bonus this is 3 times more portable, if you ever decide to write for a different platform (which might not be very far fetched since Symbian seems to loose more and more developers and even aid from Nokia)

Toad
STLPort is pretty good on Symbian now too, I hear.
Steve Jessop
+1  A: 

You're reading max. 20000 bytes of input file twice. The first read is likely to have read in the entire file. In that case the second read returns 0 bytes which you are writing out to the second file.

Also you probably don't want to use the same RFile handle for both files. It's also a good habit to Close the files when done, though the files get automagically closed also when you close your file server session.

laalto
Oh yes, that was it. cant believe how stupid I was! Many thanks for your help!
If an answer solves your problem, please accept it to keep the rep flowing. http://meta.stackoverflow.com/questions/5234/accepting-answers-what-is-it-all-about
laalto