views:

347

answers:

3

I am reading a text file which contains a word with a punctuation mark on it and I would like to read this word into a string without the punctuation marks.

For example, a word may be " Hello, "

I would like the string to get " Hello " (without the comma). How can I do that in C++ using ifstream libraries only. Can I use the ignore function to ignore the last character?

Thank you in advance.

+1  A: 

Try ifstream::get(Ch* p, streamsize n, Ch term).

An example:

char buffer[64];
std::cin.get(buffer, 64, ',');
// will read up to 64 characters until a ',' is found
// For the string "Hello," it would stream in "Hello"

If you need to be more robust than simply a comma, you'll need to post-process the string. The steps might be:

  1. Read the stream into a string
  2. Use string::find_first_of() to help "chunk" the words
  3. Return the word as appropriate.

If I've misunderstood your question, please feel free to elaborate!

Josh
Thank you very much for the explanation of how to ignore characters other than a comma. I just have a doubt. Just like my comment in the above answer. If I get the word using a char variable (buffer), how can I store the complete characters into a string (wordstring). So I could then show the word in other parts of the problem by simply calling the string (wordstring). Thank you for the quick response!
jualin
A: 

If you only want to ignore , then you can use getline.

 const int MAX_LEN = 128;
 ifstream file("data.txt");
 char buffer[MAX_LEN];

 while(file.getline(buffer,MAX_LEN,','))
 {
  cout<<buffer;
 }

EDIT: This uses std::string and does away with MAX_LEN

ifstream file("data.txt");
string string_buffer;    
while(getline(file,string_buffer,','))
{
  cout<<string_buffer;
}
Jacob
That worked like a charm, the only problem is that I would like to store the word into an string. How can I do that?
jualin
String as in `std::string`?
Jacob
I think so. I just want to store it into a string type variable. Something that I would declare as: string mystringSo I could store the word "Hello," without the comma into "mystring". Thus if I decide to call "mystring" I can just say: cout << mystringWould the EDITED part work by itself, or does it need the first part of the code? Thank you for the quick response.
jualin
One more question. What is the purpose of the "c_str" at the end of the string variable?
jualin
So, the word is stored in `std::string` now. `c_str()` returns a null-terminated sequence of characters (a C-style string). It's not required, you can directly display `std::string` with cout - I've updated it to that effect.
Jacob
Thank you very much!
jualin
+1  A: 

One way would be to use the Boost String Algorithms library. There are several "replace" functions that can be used to replace (or remove) specific characters or strings in strings.

You can also use the Boost Tokenizer library for splitting the string into words after you have removed the punctuation marks.

James McNellis
Thank you, I'll look into that.
jualin