Results 1 to 31 of 31

Thread: simple string modification

  1. #1

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197

    Arrow simple string modification

    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?
    Last edited by markman; Feb 23rd, 2002 at 07:00 PM.
    retired member. Thanks for everything

  2. #2
    New Member
    Join Date
    Jan 2002
    Posts
    13
    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..

  3. #3

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    yeah but how do I split the string by " "?
    retired member. Thanks for everything

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    1. while in the string
    2. find " "
    3. add the substring between the occurances of " " to a list
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Also:
    Code:
    istringstream iss("5 -2.5 6");
    
    float a, b, c;
    
    iss >> a >> b >> c;
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    istringstream comes from what header file?
    retired member. Thanks for everything

  7. #7
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Parksie: what exactly do istringstream and ostringstream do?
    Alcohol & calculus don't mix.
    Never drink & derive.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    #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.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    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:
    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;		
    			/////////////////////////////
    		}
    	}
    }
    I get 15 errors from the header file.
    retired member. Thanks for everything

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    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;		
    			/////////////////////////////
    		}
    	
    	}
    
    }
    retired member. Thanks for everything

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    iss = szText;
    Not sure about that one...try creating the istringstream at that point.

    What compiler errors/logic problems do you have?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Originally posted by parksie
    Also:
    Code:
    istringstream iss("5 -2.5 6");
    
    float a, b, c;
    
    iss >> a >> b >> c;
    So what exactly does this do?
    Alcohol & calculus don't mix.
    Never drink & derive.

  14. #14

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    Code:
    #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
    retired member. Thanks for everything

  15. #15

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    bump

    Hurry up! I need to know how to do this by february next year!
    retired member. Thanks for everything

  16. #16
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  17. #17
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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;
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  18. #18
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  19. #19
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Works! Here's your solution markman!

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

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  20. #20
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  21. #21
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    typedef basic_istream<char, char_traits<char> > istream;
    class istrstream : public istream // ...
    vs.

    Code:
    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
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  22. #22
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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()
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  23. #23
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    good point
    I wish the STL documentation of VC++ was better...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  24. #24
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I prefer the dinkumware documentation, it's well good (plus they sell a standard library replacement for visual c++).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  25. #25

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    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.....
    retired member. Thanks for everything

  26. #26
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    ifstream file("whatsit.txt");
    
    file >> a >> b >> c;
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  27. #27

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    I should have asked that at the beginning
    retired member. Thanks for everything

  28. #28
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Dead nice, these streams - they all work in exactly the same way
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  29. #29
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  30. #30
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It's called standards compliance, which is a big thing in a corporate environment.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  31. #31
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Documentation of VC++ Standard Library says i/ostringstream <sstream> is for string objects, i/ostrstream <strstream> is for C-style strings...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width