PDA

Click to See Complete Forum and Search --> : simple string modification


markman
Feb 23rd, 2002, 05:56 PM
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?

saridemir
Feb 23rd, 2002, 06:17 PM
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..

markman
Feb 23rd, 2002, 06:56 PM
yeah but how do I split the string by " "?

kedaman
Feb 24th, 2002, 03:21 AM
1. while in the string
2. find " "
3. add the substring between the occurances of " " to a list

parksie
Feb 24th, 2002, 03:51 PM
Also:istringstream iss("5 -2.5 6");

float a, b, c;

iss >> a >> b >> c;

markman
Feb 24th, 2002, 06:23 PM
istringstream comes from what header file?

Wynd
Feb 24th, 2002, 07:40 PM
Parksie: what exactly do istringstream and ostringstream do?

parksie
Feb 25th, 2002, 11:06 AM
#include <strstream>Stringstreams are specialisations of istream and ostream for string objects.

Instead of formatting onto stdout, they format into a string, replacing sprintf and sscanf.

markman
Feb 25th, 2002, 12:28 PM
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:

#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;
/////////////////////////////
}
}
}

I get 15 errors from the header file.

CornedBee
Feb 25th, 2002, 02:39 PM
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.

markman
Feb 26th, 2002, 12:11 PM
then how come this wont work?
using the same file as im my previous post:

#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;
/////////////////////////////
}

}

}

parksie
Feb 26th, 2002, 01:04 PM
iss = szText;Not sure about that one...try creating the istringstream at that point.

What compiler errors/logic problems do you have?

Wynd
Feb 26th, 2002, 06:07 PM
Originally posted by parksie
Also:istringstream iss("5 -2.5 6");

float a, b, c;

iss >> a >> b >> c; So what exactly does this do?

markman
Feb 27th, 2002, 11:59 AM
#include <strstream>
using namespace std;

void main() {
istringstream iss("5 -2.5 6");

float a, b, c;

iss >> a >> b >> c;
}

Even this doesnt work

markman
Mar 1st, 2002, 12:41 PM
bump

Hurry up! I need to know how to do this by february next year!

parksie
Mar 1st, 2002, 01:00 PM
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?

CornedBee
Mar 1st, 2002, 01:19 PM
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):

#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;
}

parksie
Mar 1st, 2002, 01:34 PM
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).

CornedBee
Mar 2nd, 2002, 03:09 AM
Works! Here's your solution markman!

btw parksie, istrstream comes from the new template-based standard library too, why is it bad?

parksie
Mar 2nd, 2002, 04:46 AM
istrstream isn't templated :p

http://www.dinkumware.com/htm_cpl/strstrea.html

CornedBee
Mar 2nd, 2002, 07:45 AM
typedef basic_istream<char, char_traits<char> > istream;
class istrstream : public istream // ...


vs.


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;


It's not THAT much difference, both come from basic_istream

parksie
Mar 2nd, 2002, 07:56 AM
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()

CornedBee
Mar 2nd, 2002, 01:00 PM
good point
I wish the STL documentation of VC++ was better...

parksie
Mar 2nd, 2002, 08:07 PM
I prefer the dinkumware documentation, it's well good (plus they sell a standard library replacement for visual c++).

markman
Mar 3rd, 2002, 10:41 AM
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.....

parksie
Mar 3rd, 2002, 11:40 AM
ifstream file("whatsit.txt");

file >> a >> b >> c;

markman
Mar 3rd, 2002, 03:00 PM
I should have asked that at the beginning :rolleyes:

parksie
Mar 3rd, 2002, 03:42 PM
Dead nice, these streams - they all work in exactly the same way :D

CornedBee
Mar 4th, 2002, 09:48 AM
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)

parksie
Mar 4th, 2002, 10:52 AM
It's called standards compliance, which is a big thing in a corporate environment.

CornedBee
Mar 5th, 2002, 05:29 AM
Documentation of VC++ Standard Library says i/ostringstream <sstream> is for string objects, i/ostrstream <strstream> is for C-style strings...