Reading&Writting data from/to file at same time

Login to reply to this topic.
Fri, 2006-03-31 19:51
Anonymous
Forum posts: 2043
Hi there,
belowing is the code where i read data from a file and then i encrypt
that buffer. Finally i want to overwrite  file's origional data with my encrypted buffer.
but my encrypted data is always written in the end of file. Sad(


RFs& fs = CCoeEnv::Static()->FsSession(); // 'global' file server session
RFile file;
User::LeaveIfError(file.Open(fs, aFileName, EFileRead | EFileWrite | EFileStream));
CleanupClosePushL(file);
   
const TInt bufferSize = 32;
TBuf<bufferSize> fileData;
TFileText aTxtFile;
TInt aDataRead=0;
TInt aDataWrite=0;
aTxtFile.Set(file);   
   
while(aDataRead != KErrEof)
{
aDataRead = aTxtFile.Read(fileData);
if (aDataRead != KErrEof)
{                                       
TInt length = fileData.Length();           
TBuf<bufferSize> dstBuffer;            
//encrypt
EncryptData(fileData,dstBuffer,length,key);                            
//write encrypted data to file
aTxtFile.Seek(ESeekStart);   // this i'm doing wrong as data is always written in the end of file.
   
aDataWrite = aTxtFile.Write(dstBuffer);

}

}
   
// close file and clean up
CleanupStack::PopAndDestroy(&file);

Fri, 2006-03-31 22:27
Joined: 2004-10-31
Forum posts: 92
Re: Reading&Writting data from/to file at same time
By writing encrypted data to replace the contents of the file as you read from it, you risk scribbling over unread data, I think.  Can you guarantee that encrypting 32 bytes of data will return you 32 bytes exactly?  If not, doesn't that mean you replace more (or less) than you read?

I'd be tempted to write your encrypted content to a temporary file then, when you've finished encrypting the entire file, replace the original with the temporary.
Sat, 2006-04-01 00:42
Error (not verified)
Forum posts: 2043
Re: Reading&Writting data from/to file at same time
Quote from: duckling
By writing encrypted data to replace the contents of the file as you read from it, you risk scribbling over unread data, I think.  Can you guarantee that encrypting 32 bytes of data will return you 32 bytes exactly?  If not, doesn't that mean you replace more (or less) than you read?

I'd be tempted to write your encrypted content to a temporary file then, when you've finished encrypting the entire file, replace the original with the temporary.

is there any other solution than "writing encrypted content to a temporary file & then replacing origional" Huh?
Sat, 2006-04-01 12:54
Error (not verified)
Forum posts: 2043
Re: Reading&Writting data from/to file at same time
file i'm going to read data from can be of different format (jpg/txt/audio/video e.t.c..)

may be i should use Streams or stores???

Any advice??
Mon, 2006-04-03 06:25
Joined: 2006-03-07
Forum posts: 15
Re: Reading&Writting data from/to file at same time
Hi Error
It seems that by the time you want to write the encrypted data
You got the encrypted data in dstBuffer and
Also you dont want the data of the file any more right??
then why dont you delete the file and create it again
and then write data
I know its a silly idea but..
if that helps
bye

Raja

Mon, 2006-04-03 07:21
Joined: 2006-03-31
Forum posts: 28
Re: Reading&Writting data from/to file at same time
Hi,
there is some problem in ur code.
>>TInt Seek(TSeek aMode);
It is only necessary to call this function before using Read()
because Write() always seeks to the end of the file before writing.

the Write()API`s description
"Writes the contents of a descriptor to the end of a file".

SymbianLN

Mon, 2006-04-03 09:43
Error (not verified)
Forum posts: 2043
Re: Reading&Writting data from/to file at same time
what i'm doing is :

create a new file
read data from src file
encrypt  data
write encrypted data to new created file
finally delete src file & rename new file to src file

better solutions r welcome Wink

1 Q:  this above code works fine with  .txt files , but it dont
seem working with  .jpg (havnt checked with other media files)



Mon, 2006-04-03 10:21
Joined: 2005-09-16
Forum posts: 141
Re: Reading&Writting data from/to file at same time
Hi ,

Better to do like this,

1)open file to read
2)read data into descriptor
3)close the file
4)encrypt the data
5)open file to write
6)write data into file
7)close the file

regards,
Eswar
Mon, 2006-04-03 15:42
Error (not verified)
Forum posts: 2043
Re: Reading&Writting data from/to file at same time
Quote from: eswar_illuri
Hi ,

Better to do like this,

1)open file to read
2)read data into descriptor
3)close the file
4)encrypt the data
5)open file to write
6)write data into file
7)close the file

regards,
Eswar


what if  file is large? is it good to hold large amount of data in memory  until process is complete?
  • Login to reply to this topic.