Say i had a string such as:
"1 -2.5 6"
(without the quotes) and I wanted to set the three numbers to a, b, and c. How would I do that?
Printable View
Say i had a string such as:
"1 -2.5 6"
(without the quotes) and I wanted to set the three numbers to a, b, and c. How would I do that?
I think you have to write a function that input a string and generates array and you can split the string by " " (remember strings are also array). I do not have time now if noone answer your question I will write you an sample code...
yilmaz..
yeah but how do I split the string by " "?
1. while in the string
2. find " "
3. add the substring between the occurances of " " to a list
Also:Code:istringstream iss("5 -2.5 6");
float a, b, c;
iss >> a >> b >> c;
istringstream comes from what header file?
Parksie: what exactly do istringstream and ostringstream do?
Stringstreams are specialisations of istream and ostream for string objects.Code:#include <strstream>
Instead of formatting onto stdout, they format into a string, replacing sprintf and sscanf.
how come this wont work then?
make a file Test.in with the text:
3
1 2 1
1 1 1
1 -2.5 6
then put this code in a project:
I get 15 errors from the header file.Code:#include <fstream.h>
#include <iostream.h>
#include <strstream>
struct ABC {
double a;
double b;
double c;
};
void main() {
char* inFilename = "Test.in";
char* outFilename = "Test.out";
ifstream infile(inFilename);
ofstream outfile(outFilename);
char szText[20] = "";
int i = 0;
istringstream splitter = "";
ABC vABC;
while (infile >> szText) {
i++;
if (i > 1) {
/////////////////////////////
splitter = szText;
splitter >> vABC.a >> vABC.b >> vABC.c;
cout << vABC.a << endl;
cout << vABC.b << endl;
cout << vABC.c << endl;
/////////////////////////////
}
}
}
a) to get a complete line of the file you should use getline (the global version)
b) C++ headers with .h are deprecated. You should use the ones without the .h instead. Important: NEVER mix the two types.
c) C++ headers without the .h define everything in the std namespace. You can either
- always use std:: when accessing one of the header elements (e.g. std::cout << "Hello" << std::endl;)
- use using declarations to make specific parts accessible (e.g.
using std::cout;
cout << "Hello" << std::endl; // endl is still not accessible
// you could change that with using std::endl
)
- include the whole namespace by doing
using namespace std;
after including all headers.
This should solve at least 14 of the 15 errors you have.
then how come this wont work?
using the same file as im my previous post:
Code:#include <fstream>
#include <iostream>
#include <strstream>
using namespace std;
struct ABC {
double a;
double b;
double c;
};
void main() {
char* inFilename = "Test.in";
char* outFilename = "Test.out";
ifstream infile(inFilename);
ofstream outfile(outFilename);
char szText[20] = "";
int i = 0;
istringstream iss;
ABC vABC;
while (infile.getline(szText,20,' ')) {
i++;
if (i > 1) {
/////////////////////////////
iss = szText;
iss >> vABC.a >> vABC.b >> vABC.c;
cout << vABC.a << endl;
cout << vABC.b << endl;
cout << vABC.c << endl;
/////////////////////////////
}
}
}
Not sure about that one...try creating the istringstream at that point.Code:iss = szText;
What compiler errors/logic problems do you have?
So what exactly does this do?Quote:
Originally posted by parksie
Also:Code:istringstream iss("5 -2.5 6");
float a, b, c;
iss >> a >> b >> c;
Even this doesnt workCode:#include <strstream>
using namespace std;
void main() {
istringstream iss("5 -2.5 6");
float a, b, c;
iss >> a >> b >> c;
}
bump
Hurry up! I need to know how to do this by february next year!
That's a whole 11 months ;)
I don't have a compiler at the moment so I wouldn't know :(
How about a << iss or something?
when I do
istringstream iss;
it gives me this error:
error C2079: 'iss' uses undefined class 'basic_istringstream<char,struct std::char_traits<char>,class std::allocator<char> >'
In the help, istringstream is defined as
typedef basic_istringstream<char> istringstream;
but there is no header file that contains this line. In <strstream> there is the class istrstream, which is derived from istream, which is the char-specialization of basic_istream.
So you probably must use this class.
Here is the modified code (compiles and runs with correct output on my pc):
Code:#include <strstream>
#include <iostream>
using namespace std;
void main()
{
istrstream iss("5 -2.5 6");
float a, b, c;
iss >> a >> b >> c;
cout << a << " " << b << " " << c << endl;
}
Try <sstream> I may have been thinking insanely last time...
istrstream is the old type, don't use it (it's easy to get huge memory leaks if you don't know how it works).
Works! Here's your solution markman!
btw parksie, istrstream comes from the new template-based standard library too, why is it bad?
istrstream isn't templated :p
http://www.dinkumware.com/htm_cpl/strstrea.html
vs.Code:typedef basic_istream<char, char_traits<char> > istream;
class istrstream : public istream // ...
It's not THAT much difference, both come from basic_istreamCode:template<class _E,
class _Tr = char_traits<_E>,
class _A = allocator<_E> >
class basic_istringstream : public basic_istream<_E, _Tr> // ...
typedef basic_istringstream<char, char_traits<char>, allocator<char> > istringstream;
Hmmm...didn't look like that when I looked at it.
But either way, stick to the longer version...it makes more difference in output stringstreams:
char* ostrstream::str()
as opposed to
string ostringstream::str()
good point
I wish the STL documentation of VC++ was better...
I prefer the dinkumware documentation, it's well good (plus they sell a standard library replacement for visual c++).
Ok. How would i get the string from a text file and then split it? I cant seem to be able to set the iss = the text from the text file.....
Code:ifstream file("whatsit.txt");
file >> a >> b >> c;
I should have asked that at the beginning :rolleyes:
Dead nice, these streams - they all work in exactly the same way :D
I don't see the need to buy an additional STL when I already paid for the VC++ one. (I have all this stuff for private use, no company that pays my programs)
It's called standards compliance, which is a big thing in a corporate environment.
Documentation of VC++ Standard Library says i/ostringstream <sstream> is for string objects, i/ostrstream <strstream> is for C-style strings...