Writing To Certain Lines

This is a discussion on Writing To Certain Lines within the C++ Programming forums, part of the General Programming Boards category; Hi, Let's say if have the following text file: Code: Line 1 Line 2 Line 3 Line 4 ... Line ...

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

    Writing To Certain Lines

    Hi,

    Let's say if have the following text file:
    Code:
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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
    Posts
    271
    Or try perl:
    Code:
    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, 06:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21