programmatically replace characters in a text file?

Discussion in 'C++' started by cheesywillie@gmail.com, Apr 15, 2007.

  1. Guest

    How would this be done? For example find every "a" and replace them
    with a "b".

    Sorry if i am not being descriptive enough, but i don't know how else
    to phrase the question. If you need any additional information just
    ask.

    Thanks for reading.
     
    , Apr 15, 2007
    #1
    1. Advertising

  2. wrote:
    > How would this be done? For example find every "a" and replace them
    > with a "b".
    >
    > Sorry if i am not being descriptive enough, but i don't know how else
    > to phrase the question. If you need any additional information just
    > ask.


    Open the file for both read and write
    While the file is OK
    Read some bytes from the file into a buffer
    Replace chars in the buffer as needed
    Position the file pointer back
    Write the buffer back into the file
    End-while

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 15, 2007
    #2
    1. Advertising

  3. James Kanze Guest

    On Apr 15, 4:24 pm, wrote:
    > How would this be done? For example find every "a" and replace them
    > with a "b".


    std::replace_copy( std::istreambuf_iterator< char >( source ),
    std::istreambuf_iterator< char >(),
    std::eek:streambuf_iterator< char >( dest ),
    'a',
    'b' ) ;

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Apr 15, 2007
    #3
  4. Guest

    On Apr 15, 10:24 am, wrote:
    > How would this be done? For example find every "a" and replace them
    > with a "b".
    >
    > Sorry if i am not being descriptive enough, but i don't know how else
    > to phrase the question. If you need any additional information just
    > ask.
    >
    > Thanks for reading.

    Depending on what system you are on, there are utilities that can
    do this w/o any programming. For instance, check out the unix
    utility tr.
     
    , Apr 15, 2007
    #4
  5. Guest

    Ok thanks alot for all of the info :)
     
    , Apr 16, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    768
  2. Replies:
    7
    Views:
    1,693
  3. mscir
    Replies:
    0
    Views:
    316
    mscir
    Oct 12, 2005
  4. Chris Angelico
    Replies:
    9
    Views:
    233
    Andrew Cooper
    Jul 29, 2012
  5. Tim Chase
    Replies:
    10
    Views:
    385
    Robert Miles
    Aug 31, 2012
Loading...

Share This Page