Using replace()

This is a discussion on Using replace() within the C++ Programming forums, part of the General Programming Boards category; this code won't compile: Code: char word[256]; ifstream in("test_data.txt"); in.getline(word, 256); replace(word.begin(),word.end(), ',' , ' '); in.close(); what it tries ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    124

    Using replace()

    this code won't compile:
    Code:
    char word[256];
    ifstream in("test_data.txt");
    in.getline(word, 256);
    replace(word.begin(),word.end(), ',' , ' ');
    in.close();
    what it tries to do is to replace all the commas in word with spaces...if i define word as a string it works...but then getline doesn't work...

    Thanks!

    Farooq

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    begin and end are member functions for STL container objects, a simple character array is not one of those. If you want to use a character array and the replace function you will need to do it this way:

    Code:
    char word[256];
    ifstream in("test_data.txt");
    in.getline(word, 256);
    replace(word,word+256, ',' , ' ');
    in.close();
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>if i define word as a string it works...but then getline doesn't work...
    You just have to use the template version of getline:
    Code:
    std::string word;
    std::ifstream in("test_data.txt");
    std::getline(in, word);
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    thanks guys...ill work on this...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replace Array
    By SARAHCPS in forum C Programming
    Replies: 9
    Last Post: 11-15-2005, 10:07 AM
  2. Replace a list with a new entry?
    By niponki in forum C Programming
    Replies: 4
    Last Post: 08-17-2005, 10:41 AM
  3. Replies: 5
    Last Post: 05-25-2004, 04:36 PM
  4. How to replace strings in a file?
    By Doh in forum C Programming
    Replies: 6
    Last Post: 11-26-2002, 11:16 AM
  5. Need a new way to replace a constant
    By RustGod in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2001, 02:05 PM

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