Link to home
Start Free TrialLog in
Avatar of hey_yuvi
hey_yuvi

asked on

CStdioFile

I am using the CStdioFile Class to modify a file.
But when i use the WriteString function it replaces the characters in the file. How can I add content to the file without replacing it.
Or how can i modify a single line alone
Avatar of AlexFM
AlexFM

Use SeekToEnd function to set file pointer to the end of file. After this call WriteString.
Hi,
 You must notice that , if the file contents is binary, you must write binary values. When you write a string, the ASCII codes are written.
To write a binary value use:

MyString.Format("%c", x);//x is what you want to write

Then write the string into the file.

Good Luck!
i think u have to read file in a string
use replace function of that string
and again write the string to the file
Avatar of hey_yuvi

ASKER

The problem is that when i write a string it replaces the number of bytes of that string in the file without any concern to what line the text is . eg
the contents of file are

Line 1
Line 2
Line 3
Line 4

Now i move to Line 2 using ReadString functions and then give
WriteString("New Line 2");
then the whole file changes as

Line 1
Line 2
New Line 2
4

So it replaces string in next line also
How to avoid this




Did you read my comment?
Hi,
After you use ReadString, the file poiter moves to end of line 2. After using WriteString, the contents are writte over line3. To avoid it, use
Seek function to point to begin of line 2 again.

Good Luck!
Hi,
 Thanks For ur response.
I think i am bothering u again.
I want to insert a line to the file and not at the end. so i cant use SeekToEnd()

Similarly when my new string's length is greater than the one in the file,
the next line also gets replaced , which i want to avoid.!!

ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial