Using CStdioFile to modify text file
-
Hey all. I've got a text file that I am trying to modify, but am running into some problems. Here's the code that I am using: CStdioFile myFile; myFile.Open("c:\\temp\\test1.txt",CFile::modeReadWrite); myFile.Seek(40,CFile::begin); myFile.WriteString("this is the replaced line\n"); myFile.Close(); It kind of works, but what is happening is it is overwriting the contents of the next lines and I can't figure out how to get around this. Ie, the file looks like this before I run it line 0 line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 then, after i execute the above statements line 0 line 1 line 2 line 3 line 4 this is the replaced line e 8 line 9 So, when the newly modified text is written, if it happens to be longer than the original line, it will write over other lines below it, instead of "appending" so to speak, making the file look like: ... line 3 line 4 this is the replaced line line 6 line 7 line 8 ... if I do something like "...WriteString("line Z"), it replaces it fine because the new string matches the length of the one that's in the file. Any ideas on how to replace an entire line in a text file (each line is ended with \n) without overwriting following lines? Thanks
-
Hey all. I've got a text file that I am trying to modify, but am running into some problems. Here's the code that I am using: CStdioFile myFile; myFile.Open("c:\\temp\\test1.txt",CFile::modeReadWrite); myFile.Seek(40,CFile::begin); myFile.WriteString("this is the replaced line\n"); myFile.Close(); It kind of works, but what is happening is it is overwriting the contents of the next lines and I can't figure out how to get around this. Ie, the file looks like this before I run it line 0 line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 then, after i execute the above statements line 0 line 1 line 2 line 3 line 4 this is the replaced line e 8 line 9 So, when the newly modified text is written, if it happens to be longer than the original line, it will write over other lines below it, instead of "appending" so to speak, making the file look like: ... line 3 line 4 this is the replaced line line 6 line 7 line 8 ... if I do something like "...WriteString("line Z"), it replaces it fine because the new string matches the length of the one that's in the file. Any ideas on how to replace an entire line in a text file (each line is ended with \n) without overwriting following lines? Thanks
U have to store remaining lines( after 4 line) in some other temporary buffer , then insert what ever content u want insert , once again append temporary buffer.