Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting new line in the middle of a existing file

 
Roy Sin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
is there a way that i am able to insert a new line in the middle of an EXISTING text file ???
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you have several choices.
You can read in the file, one line at a time, and write it to a temp file, and at the correct time shove your own line in there.
When finished reading in the original file, write the temp file back out to it.
You could read in input file into an array, and do an insert at the correct place. This could be the beginning of an Editor of some sort if you stuck on a GUI.
If the text file is small enough, you could read it into one giant string. Then sub-string the part before the addition, add in your stuff, and concatenate in the last part of the string.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic