Reading and Writing to a file at the same time
-
waxie wrote:
we can only read to a file first, close it, then write to it,
Who said this ? we can both, read and write to a file at the same ..For this u must open the file in read-write mode.. nave
-
Hi guys, I have been teaching myself c++/mfc, and I was wondering if there is a way to read and write to a file at the same time? Like read the file first and if it encounters this text, replace that text with a new text (so write to the file)? I observed that we can only read to a file first, close it, then write to it, or vice versa. But is there a way to do both? I did not want to use fseek() since my number of bytes per line are not fixed. I'd appreciate all the help I can get. And if I got a concept wrong, please do correct me. Thanks a lot!:-D :rose:The Buggy
Hello. You can see a file as a number of bytes. There are no intervening spaces or "records" in it. You can open a file for both reading and writing, but then you (usually) has to fseek between read and writes. You can overwrite parts of a file, but you can't insert (or delete) new bytes in the beginning or middle of an existing file. (Acually, you can, but that means you have to write the "move" code yourself, and it won't be very effective). The only way you can change an existing file is at the end. You can append to the file or remove (by setting the file length to a shorter value than before. The easiest way to change the contents of a file (other than overwriting a fix number of bytes) is to create a temp file, and then rename the temp file to the name of the original file.
-
if ur using CFile in MFC open the file as follows.. CFile File; File.Open( _T("C:\\out.txt"),CFile::modeReadWrite ); Now u can use both Read and Write function CFile.. nave
-
I wonder... why doesn't this code work? CString csCurrLine; while(oFile.ReadString(csCurrLine)) { if(csCurrLine==_T("String")) { oFile.WriteString(_T("Overwrite String")); break; } } oItemListFile.Close(); TheBuggy
First: Beacuse the file pointer is placed on the character after "String" in the file, so that's where you will be writing "Overwrite String" You will have to position the file pointer to the beginning of "String" before writing to the file. Second: Since "Overwrite String" is longer than "String", if the file pointer was placed at the beginning of "String", the "String " would be overwritten, *but* also the 10 characters after "String" in the file. Pls read my post.
-
I wonder... why doesn't this code work? CString csCurrLine; while(oFile.ReadString(csCurrLine)) { if(csCurrLine==_T("String")) { oFile.WriteString(_T("Overwrite String")); break; } } oItemListFile.Close(); TheBuggy
Can u give more details.. What happened? did the execution comes to
oFile.WriteString(_T("Overwrite String"));
.. more over.. When u read usinf ReadString, the filepointer advance to the next line..So before writting u musk seek back to the staring of the line that u have to replace.. nave -
First: Beacuse the file pointer is placed on the character after "String" in the file, so that's where you will be writing "Overwrite String" You will have to position the file pointer to the beginning of "String" before writing to the file. Second: Since "Overwrite String" is longer than "String", if the file pointer was placed at the beginning of "String", the "String " would be overwritten, *but* also the 10 characters after "String" in the file. Pls read my post.
-
Yeah, OK, say the pointer is really not in the right position. But why doesn't my 'overwriting' string appear in any parts of the text file? Am I correct to assume that it should appear? Say, on the next line? Thanks so much for all your replies.
If you did open the file for reading and writing (see Naveen's post), then it should be writtened to the file, immediately after "String". -- modified at 4:51 Tuesday 16th May, 2006 That is, provided the expression if(csCurrLine==_T("String")) evaluates to a value != 0
-
If you did open the file for reading and writing (see Naveen's post), then it should be writtened to the file, immediately after "String". -- modified at 4:51 Tuesday 16th May, 2006 That is, provided the expression if(csCurrLine==_T("String")) evaluates to a value != 0
-
Honestly, it really does not work. My new string does not appear anywhere in the file - or as expected, after the old string.
-
OK, I beleive you! :) I can think of two things: First: Does the expression if(csCurrLine==_T("String")) evaluate to a value != 0 That is, does your program find "String" in your present file? Second: Do you check the return code of the write-statement?
-
Yes, the program finds the evaluated string. I'm sure with that. And yes, it can write coz i tried writing to a file without reading and it does write. WriteString doesn't have a return value - it returns void.
-
I wonder... why doesn't this code work? CString csCurrLine; while(oFile.ReadString(csCurrLine)) { if(csCurrLine==_T("String")) { oFile.WriteString(_T("Overwrite String")); break; } } oItemListFile.Close(); TheBuggy
Why does
WriteString()
fail?TRY
{
oFile.WriteString(_T("Overwrite String"));
}
CATCH(CFileException, pEx)
{
AfxMessageBox(_T("Exception encountered."));
}
END_CATCH
"The largest fire starts but with the smallest spark." - David Crow