Thread: stringstreams help i know you guys will know

  1. #1
    Registered User sswaters's Avatar
    Join Date
    Oct 2005
    Posts
    18

    stringstreams help i know you guys will know

    I have a program some of you may remember my previous post. It uses a stringstream to convert an int to a string.
    The problem is i cant find how to empty the string after it is used once
    Code:
    converter << filenumber;  // tacks the number onto the stream
    num = converter.str();  // turn stream into a string
    I want the stringstream to not keep the number from the previous while loop run is there like a empty stringstream command.
    King of the overly complicated code

  2. #2
    Registered User sswaters's Avatar
    Join Date
    Oct 2005
    Posts
    18
    oh and if anyone can tell me how to not skip spaces with ifstream that would help too
    King of the overly complicated code

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >is there like a empty stringstream command.
    If I remember correctly, this solution was posted a few days ago.
    Code:
    converter.str("");

  4. #4
    Registered User sswaters's Avatar
    Join Date
    Oct 2005
    Posts
    18
    thanks it works i dont no why i didnt think of that any solution to the ifstream needs to not skip spaces problem
    King of the overly complicated code

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >oh and if anyone can tell me how to not skip spaces with ifstream that would help too
    One idea is to use getline(), which will read a whole line, or if you supply the third parameter to getline(), until it sees the supplied delimiter.
    Code:
    string stuff;
    getline(in, stuff);
    Code:
    string stuff;
    //Read until a comma is encountered
    getline(in, stuff, ',');

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) The stringstream str() function can be used to get or set the string in the stringstream:
    Code:
    ostringstream out;
    out<<10;
    cout<<out.str()<<endl;
    
    out.str("");
    cout<<"--->"<<out.str()<<"<----"<<endl;
    2) The ">>" operator in a line like this:

    int num;
    inFile>>num;

    is programmed to stop reading when it encounters whitespace, and then to skip leading whitespace for the next read. So, if you want to read in spaces, you can't use ">>". Since you want to read in spaces, which are characters, you need to read the data into a string:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    
    	ifstream inFile("C:\\TestData\\data.txt");
    	if(!inFile)
    	{
    		cout<<"Failed to open file."<<endl;
    		return 1;
    	}
    	
            string wholeLine = "";
    	while( getline(inFile, wholeLine) ) 
    	{
    		cout<<wholeLine<<endl;
    	}
    
        return 0;
    }
    Make sure your read statement is used as the while conditional--that will make sure things end correctly when you get to the end of the file or if there is an error while trying to read the file. getline() returns an object that will evaluate to false if there is an error or if the end of file has been reached(which is also considered an error).
    Last edited by 7stud; 10-22-2005 at 01:35 AM.

  7. #7
    Registered User sswaters's Avatar
    Join Date
    Oct 2005
    Posts
    18
    getline(in, stuff, ',');
    what are in and stuff representing
    i want to move it into a string called cvk
    would that be
    getline(inputfile, cvk, ',');
    or
    getline(cvk,inputfile, ',');

    edit:never mind i see it would be
    getline(inputfile, cvk, ',');

    thanks for all the help
    Last edited by sswaters; 10-22-2005 at 01:29 AM.
    King of the overly complicated code

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >edit:never mind i see it would be
    >getline(inputfile, cvk, ',');

    Correct. The first parameter is the input stream, the second parameter is the string to read into, and the third parameter is the optional delimiter. And when you use char arrays instead of strings, it would be:
    Code:
    inputfile.getline(array, sizeof(array), ',');
    But strings are definitely a better choice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey guys, I'm new!
    By MrDoomMaster in forum C++ Programming
    Replies: 15
    Last Post: 10-31-2003, 05:47 PM
  2. How long have you guys been working with C/C++??
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 08-01-2003, 03:41 PM
  3. Tic Tac Toe -- Can you guys rate this please?
    By Estauns in forum Game Programming
    Replies: 2
    Last Post: 09-15-2001, 10:22 AM
  4. hello guys
    By lupi in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2001, 06:42 AM
  5. hello guys
    By lupi in forum C++ Programming
    Replies: 1
    Last Post: 09-09-2001, 01:00 AM