CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: replace word

  1. #1
    Join Date
    Aug 2004
    Posts
    34

    replace word

    if line is read from a file and is,
    string line = "weather,is,cold";

    how to replace word "cold" with "hot" and write it back to same line
    in same file?. ->"weather,is,hot";

    using seekp()??

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757

    Re: replace word

    Given that the word you want to replace is <= length than what you want to replace, one solution is file-mapping. Otherwise, you generally you have to use a temp file.

    Kuphryn

  3. #3
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582

    Re: replace word

    Another option is to use the external utility sed. It's been ported to nearly everything. If you haven't used sed, you haven't lived.

    Another is to read the whole file in, replacing as you go, and then write it back out. This is probably identical to using a temp file for small files.

    Jeff

  4. #4
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: replace word

    For small files, I find it most convenient to re-write the entire file, as others suggested already.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured