Thread: Writing To Certain Lines

  1. #1
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007

    Writing To Certain Lines


    Let's say if have the following text file:
    Line 1
    Line 2
    Line 3
    Line 4
    Line 10
    Is there any way I could add something "Line 10" so it said something like "This is Line 10", but nothing else? Basically what I'm asking is how to add things to certain lines instead of just appending to a file. Can anyone help me? Thanks.

    BTW, if possible I would prefer fstreams (I'm not sure what else there is), but if not that's ok.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    Since each line can be of varying length, there is no way to just go to a specific line and insert information there -- all the data following that line has to be moved in the file to make room.

    Generally, line-based processing programs do their work by reading a line at a time from an input source, doing something to each line, and outputting the new lines to a new file. In other words, "in-place" editing of text files is not possible without a lot of buffering.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Or try perl:
    perl -ip -e 's/^(.+)$/This is \1/' FILENAME

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Blank lines "\n"
    By Coding in forum C++ Programming
    Replies: 15
    Last Post: 02-18-2008, 08:56 PM
  2. counting number of lines in my output file
    By zesty in forum C Programming
    Replies: 34
    Last Post: 12-25-2007, 09:15 PM
  3. Discarding blank lines?
    By Blurr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 12:30 PM
  4. Question About Reading Lines from stdin
    By Zildjian in forum C Programming
    Replies: 7
    Last Post: 11-12-2003, 05:45 AM
  5. Writing Bitmap Files
    By HappyDude in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2001, 05:48 PM