Jump to content

StreamWriter replace line?

- - - - -

  • Please log in to reply
1 reply to this topic

#1
cypro

cypro

    Newbie

  • Members
  • Pip
  • 5 posts
Hello to evreyone i am using Visual Studio 2008 for native program project.

I am using StreamReader and StreamWriter to read and write to file my question is, how can i replace a line with new value without change line.

e.g myfile.txt
blue
red
yellow

and to change to
green
red
yellow

The value is not the same but i want to replace and not to delete and writer new because is organized by ID!
Thank you

#2
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
First you need to read the contents of the file line by line. Then if it contains a certain text replace it with your new text.

ReadFileByLine
If Line.Contains("Blue")
Line = Line.Replace("Blue", "Green")

The above code is obviously not a working example but I hope you get the point. :)

~ Committed.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users