getline() for string

This is a discussion on getline() for string within the C++ Programming forums, part of the General Programming Boards category; Is there any function to read one line from a string? Code: #include <string> std::string str, str2; std::getline(cin, str, 'p'); ...

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231

    getline() for string

    Is there any function to read one line from a string?
    Code:
    #include <string>
    
    std::string str, str2;
    std::getline(cin, str, 'p');
    //Now I want read one line of it and store it in str2, how?
    I know its a little stupid, I searched MSDN.

    Thanks
    Last edited by siavoshkc; 08-19-2006 at 01:21 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    //Now I want read one line of it and store it in str2, how?
    There's only one line in str, so I guess it would be.
    Code:
    str2 = str;

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Wow, sorryyyyyyyyy. I am going to edit it.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,461
    You can either read until you find a '\n', or you can read it as if it was a stream with <stringstream>
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    Maybe you can combine substr and find. This looks like it works.
    Code:
    #include <iostream>
    #include <string>
    
    using std::cout;
    using std::string;
    
    int main()
    {
        string str = "Hi there!\nI'm happy!";
        string str2 = str.substr(0, str.find('\n'));
    
        cout << "First line = " << str2 << "\n";
        cout << "Second line = " << str.substr(str.find('\n') + 1) << "\n";
    
        return 0;
    }

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Maybe you can combine substr and find.
    I know. I usually don't work with string and less than it string stream. I am serching for a function to read a line. That seems impossible for string. So I am going to use string stream.
    How can I read from cin to string stream? I think I knew these things some day, but I can't remember.

    [edit]
    I read them somewhere didn't learn them really.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  7. #7
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    Quote Originally Posted by siavoshkc
    How can I read from cin to string stream?
    I know how to do that! I was reading about it just yesterday and thought it was really neat. I don't really know how it works though.
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    
    int main()
    {
        std::stringstream stream;
        std::string str2;
    
        stream << std::cin.rdbuf();
        std::getline(stream, str2);
        std::cout << str2 << "\n";
    
        return 0;
    }

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    I don't really know how it works though.
    No problem I know. Reads input buffer and puts it into stream then reads one line from stream and puts it into str2.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,461
    getline works with stringstreams the same way it does with cin. This is so because the first parameter is a reference to an input stream.

    Code:
    #include <sstream>
    #include <iostream> 
    #include <string>
    
    int main() {
        std::istringstream istr;
        std::string str;
        std::getline(istr, str, 'p');
    }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    rdbuf() don't work because there is no delim and nothing will come to string from keyboard.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,461
    http://www.cplusplus.com/ref/iostream/ios/rdbuf.html

    That explains how to use rdbuf(). You first need to associate your stream with the correct buffer
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Code:
    stream << std::cin.rdbuf();
    std::getline(stream, str2);
    std::cout << str2 << "\n";
    It gets cin streambuf and puts it into stream. But this code won't work as we want because there is no delim for cin to put characters into streambuf. And because it never reaches EOF, it will get keys from keyboard forever.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  13. #13
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    Quote Originally Posted by siavoshkc
    Code:
    stream << std::cin.rdbuf();
    std::getline(stream, str2);
    std::cout << str2 << "\n";
    It gets cin streambuf and puts it into stream. But this code won't work as we want because there is no delim for cin to put characters into streambuf. And because it never reaches EOF, it will get keys from keyboard forever.
    I faked EOF by typing ctrl+z on my Windows computer, but I don't know how to get it to work any other way. Sorry. I think if you want to stop at something like 'p' from cin, you need to read a string from cin, then use the string to make a stringstream, then read from the stringstream with a different delim.
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    
    using std::string;
    using std::getline;
    
    int main()
    {
        string str;
        string str2;
    
        getline(std::cin, str, 'p');
        
        std::stringstream stream(str);
    
        getline(stream, str2);
        std::cout << str2 << "\n";
    
        return 0;
    }
    Is that what you wanted?

  14. #14
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    I used ctrl+z but it works crazy. It goes crazy after pressing a key. It is cin.get() that losses its functionality after reading rdbuf().
    Code:
                    stringstream inp;
    	
    	inp << cin.rdbuf();
    	
    	cin.get();
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  15. #15
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    Quote Originally Posted by siavoshkc
    I used ctrl+z but it works crazy. It goes crazy after pressing a key. It is cin.get() that losses its functionality after reading rdbuf().
    Code:
                    stringstream inp;
    	
    	inp << cin.rdbuf();
    	
    	cin.get();
    Ah, isn't there a flag in cin that's set when it sees EOF? This is just a guess, but I bet it would work if you cleared it. I had that problem with a guessing game I wrote last week, and using cin.clear() fixed it all up.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 10:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 01:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21