extracting integers from a string.

D

dor

i have an input file named input.txt where all the data looks like
this:
(4,10) 20
(5,3) 13
(7,19) 6
..
..
..

the numbers are random. i need to use every number in each line
individually as an integer. i know how to use ifstream to get every
line into a string variable, but i dont know how to manipulate it to
get three different int variables out of that one string variable.
so my string is string line = "(4,10) 20";
what do i do?
can somebody help me please?
 
M

Michiel.Salters

dor said:
i have an input file named input.txt where all the data looks like
this:
(4,10) 20
(5,3) 13
(7,19) 6
.
.
.

the numbers are random. i need to use every number in each line
individually as an integer.

I doubt it. I guess you really should have a class that contains
exactly three
integers, and create one instance of that class per line. Let's call it
class X;
i know how to use ifstream to get every
line into a string variable, but i dont know how to manipulate it to
get three different int variables out of that one string variable.
so my string is string line = "(4,10) 20";

What would you do if you'd have a std::istream& operator<<
(std::istream&, X) ?

HTH,
Michiel Salters
 
J

Jim Langston

dor said:
i have an input file named input.txt where all the data looks like
this:
(4,10) 20
(5,3) 13
(7,19) 6
.
.
.

the numbers are random. i need to use every number in each line
individually as an integer. i know how to use ifstream to get every
line into a string variable, but i dont know how to manipulate it to
get three different int variables out of that one string variable.
so my string is string line = "(4,10) 20";
what do i do?
can somebody help me please?

look at std::string's find_first_of and related functions.

I.E. this outputs "4":

std::string MyString = "(4,10) 20";
std::cout << MyString.substr(1, MyString.find_first_of(",") - 1);

then if you took the substr of the find_first_of + 1 until the first ) - 1
you would get the 10.

etc..

To convert them to numbers you have your choice of ways. My favorite way is
like this:

#include <sstream>
template<typename T, typename F > T StrmConvert( F from )
{
std::stringstream temp;
temp << from;
T to = T();
temp >> to;
return to;
}

Used like this:
RefreshRate = StrmConvert<int>( Refresh );
where Refresh is a std::string

this StrmConvert is quite useful and can convert int/float/std::string
etc... to/from each other.

This is not the only way, but is the way I would do it. If you have any
more question ask.
 
D

Daniel T.

"dor said:
i have an input file named input.txt where all the data looks like
this:
(4,10) 20
(5,3) 13
(7,19) 6
.
.
.

the numbers are random. i need to use every number in each line
individually as an integer. i know how to use ifstream to get every
line into a string variable, but i dont know how to manipulate it to
get three different int variables out of that one string variable.
so my string is string line = "(4,10) 20";
what do i do?
can somebody help me please?

istream& read( istream& in, int& a, int& b, int& c ) {
int ta, tb, tc;
char ch;
if ( ! ( in >> ch && ch == '(' &&
in >> ta >> ch && ch == ',' &&
in >> tb >> ch && ch == ')' &&
in >> tc ) )
in.clear( ios_base::badbit );
else {
a = ta;
b = tb;
c = tc;
}
return in;
}
 
D

Daniel T.

"Jim Langston said:
look at std::string's find_first_of and related functions.

I.E. this outputs "4":

std::string MyString = "(4,10) 20";
std::cout << MyString.substr(1, MyString.find_first_of(",") - 1);

then if you took the substr of the find_first_of + 1 until the first ) - 1
you would get the 10.

etc..

To convert them to numbers you have your choice of ways. My favorite way is
like this:

#include <sstream>
template<typename T, typename F > T StrmConvert( F from )
{
std::stringstream temp;
temp << from;
T to = T();
temp >> to;
return to;
}

Used like this:
RefreshRate = StrmConvert<int>( Refresh );
where Refresh is a std::string

this StrmConvert is quite useful and can convert int/float/std::string
etc... to/from each other.

This is not the only way, but is the way I would do it. If you have any
more question ask.

That is certainly one way to do it. (I came up with about 5 different
ways before posting, including the above.) I didn't like the idea of
reading from the stream, then putting parts of the data back into a
stream just to read it out again.

That said, one method I cam up with was:

bool not_digit( char c ) {
return ! isdigit( c );
}

istream& read( istream& in, int& a, int& b, int& c ) {
string str;
getline( in, str );
replace_if( str.begin(), str.end(), &not_digit, ' ' );
stringstream ss( str );
ss >> a >> b >> c;
return in;
}

But the above doesn't check the format of the input file.

and of course there is the C-ish way:

istream& read( istream& in, int& a, int& b, int& c ) {
string str;
getline( in, str );
if ( sscanf( str.c_str(), "(%d,%d) %d", &a, &b, &c ) != 3 )
in.clear( ios_base::badbit );
return in;
}

But there's some duplication in the above I don't like.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top