[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 893
  • Last Modified:

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
0
hey_yuvi
Asked:
hey_yuvi
  • 2
  • 2
  • 2
  • +2
1 Solution
 
AlexFMCommented:
Use SeekToEnd function to set file pointer to the end of file. After this call WriteString.
0
 
Nass89Commented:
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!
0
 
Amritpal SinghCommented:
i think u have to read file in a string
use replace function of that string
and again write the string to the file
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
hey_yuviAuthor Commented:
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




0
 
AlexFMCommented:
Did you read my comment?
0
 
Nass89Commented:
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!
0
 
hey_yuviAuthor Commented:
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.!!

0
 
AndyAinscowCommented:
Create a temporary file.
Read string from file 1, write to temporary file (along with changes).
repeat above line until finished.
delete original file.
rename temporary file to that of the original.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now