How do I remove a specific character.. when finished reading a file..

This is a discussion on How do I remove a specific character.. when finished reading a file.. within the C++ Programming forums, part of the General Programming Boards category; Brief history about me. Hey, Kind of Noob and started programming recently. My situation I have file that has content ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    22

    Thumbs up How do I remove a specific character.. when finished reading a file..

    Brief history about me.
    Hey, Kind of Noob and started programming recently.

    My situation
    I have file that has content written as:

    Code:
    25_Cents 15_Cents 40_Cents
    14_Inches 6_Inches 18_Inches
    Cricket Rugby Football
    In_the_east  In_the_north In_the_south
    Stupid Idiot Nothing
    60_Years 72_Years 90_Years
    South_Pole West_Pole East_Pole
    How Would I remove the _ (Underscore) so they appear as:

    25 Cents 15 Cents 40 Cents
    When I print them onto screen..

    Thanks in advance
    Last edited by Swadesh; 04-18-2012 at 04:47 PM.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Take the Input line by line into std::string objects with the std::getline function.
    Then apply this standard algorithm:
    Code:
    std::replace_if(foo.begin(),foo.end(), [](char x){return x=='_';} ,' ');
    ..where foo is your string.

    If your compiler does not support C++11, just use a normal loop.
    Code:
    for(int i=0;i<foo.length();++i)
        if(foo[i]=='_')foo[i]=' ';
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    22

    Thumbs up

    what if I am storing file contents in such way... How would I do it then:

    Code:
    while (!fakeans.eof())   
     {
            for (int i=0; i<MAXQUES; i++)
            {
                fakeans >> questions[i].FakeAnswer1 >> questions[i].FakeAnswer2 >> questions[i].FakeAnswer3;
            
            }
     }
    
        fakeans.close();

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Quote Originally Posted by Swadesh View Post
    what if I am storing file contents in such way...
    Get the underscores into a char variable and forget about it.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    22
    Quote Originally Posted by manasij7479 View Post
    Get the underscores into a char variable and forget about it.
    Could you elaborate more please.., am quiet a noob

    Thanks
    Last edited by Swadesh; 04-18-2012 at 05:56 PM.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    22
    Hey manasij7479
    The first method you said works well, doesnt matter if I am getting line or using the >> operators

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from file with specific widths.
    By looza in forum C Programming
    Replies: 2
    Last Post: 11-03-2011, 12:56 PM
  2. Remove a specific char and remove multiple spaces
    By stam in forum C++ Programming
    Replies: 9
    Last Post: 12-18-2010, 06:50 AM
  3. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  4. Replies: 6
    Last Post: 08-04-2003, 10:57 AM
  5. Need help reading in specific type of file
    By Natase in forum C Programming
    Replies: 4
    Last Post: 09-12-2001, 08:02 PM

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