Reading&Writting data from/to file at same time
| Fri, 2006-03-31 19:51 | |
|
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. (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); |
|






(
Forum posts: 92
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.
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"
may be i should use Streams or stores???
Any advice??
Forum posts: 15
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
Forum posts: 28
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
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
1 Q: this above code works fine with .txt files , but it dont
seem working with .jpg (havnt checked with other media files)
Forum posts: 141
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
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?