Question about istream::get() and delimiting character
how does get() work with a delimiting character?
I tryed
ifstream is;
chat str[256];
is.get(str,',');
cout<<str;
it just prints the whole file instead of stoping at the first ','
Why is that?
then I tyred
ifstream is;
chat str[256];
is.get(str,30,',');
cout<<str;
and it looks like it just prints randomly.Besides that the string can be any size becasues its a file name
Only the three argument overload of istream::get() uses a delimiter. When you call the two argument function with ',' as a second argument it actually converts ',' to its numeric value and uses that as the length of the buffer.
Thanks that makes since.
so in this example
{1,2)
I want to start reading at the '{'
and stop reading at the ',' then continue reading tell '}'
so 1 will go to var1 and 2 will go to var2.
or in this one
{bitmap.bmp,bitmapmask}
or this one
{0}
I want to be able to start reading from '{'
and if a ','aperas then I know there is two or moer varibles on that line
and they end at '}'
I can get the first varible but not sure how to get more then that using
is.get(str,256,',');
thanks for the help
so in this example
{1,2)
I want to start reading at the '{'
and stop reading at the ',' then continue reading tell '}'
so 1 will go to var1 and 2 will go to var2.
or in this one
{bitmap.bmp,bitmapmask}
or this one
{0}
I want to be able to start reading from '{'
and if a ','aperas then I know there is two or moer varibles on that line
and they end at '}'
I can get the first varible but not sure how to get more then that using
is.get(str,256,',');
thanks for the help
I almost have the solustion
this is what I got so far
I just need a way to see if there is and extra ',' with an unknown amout of data to the next one
for example
{12,33343,1}
I know how to get the first one but how can I test for the second one? becasue it wont always be there
this is what I got so far
d = mInputFile.peek(); if(d =='{') { mInputFile.get(str,256,','); cout<<str+1<<endl; mInputFile.get(str,256,'}'); cout<<str+1<<endl; }
I just need a way to see if there is and extra ',' with an unknown amout of data to the next one
for example
{12,33343,1}
I know how to get the first one but how can I test for the second one? becasue it wont always be there
Quote:Original post by kingpinzs
Besides that the string can be any size becasues its a file name
Then use an actual string. That way, you don't have to worry about sizes.
ifstream is;string str;getline(is, str, ',');cout << str;
Notice this is the free function std::getline, not the member function .getline() of the stream object.
You could look into the boost tokenizer for this.
As for a purely SC++L solution I think your proposed method looks a bit complicated and not particularly robust; I'd probably do it this way:
The limitation here is that you can't have spaces, they get treated like commas, but you seem to be avoiding spaces anyway.
As for a purely SC++L solution I think your proposed method looks a bit complicated and not particularly robust; I'd probably do it this way:
#include <iostream>#include <string>#include <sstream>#include <algorithm>#include <functional>#include <limits>int main(){ // using a string stream simply to represent your file std::istringstream file("... { foo,bar, baz , qux} ..."); // eat text till '{' file.ignore(std::numeric_limits<std::streamsize>::max(), '{'); // read text till '}' std::string text; std::getline(file, text, '}'); // replace commas with whitespace so that only whitespace separates tokens std::replace_if(text.begin(), text.end(), std::bind2nd(std::equal_to<char>(), ','), ' '); // extract tokens to output stream, but it could be to a std::vector or something std::istringstream tokens(text); std::copy(std::istream_iterator<std::string>(tokens), std::istream_iterator<std::string>(), std::ostream_iterator<std::string>(std::cout, "\n"));}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement