Sign in to follow this  

Question about istream::get() and delimiting character

This topic is 3154 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
I almost have the solustion

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:

#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"));
}
The limitation here is that you can't have spaces, they get treated like commas, but you seem to be avoiding spaces anyway.

Share this post


Link to post
Share on other sites

This topic is 3154 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this