Thread: what's wrong with my syntax? (passing text stream to a function)

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    14

    what's wrong with my syntax? (passing text stream to a function)

    Hi everybody,

    I have some 50 bytes lines in a text file containing something like this:
    Code:
    0 111444550 sam        fisher               -1
    I want to pass it to a function to parse it. Looked like my best bet was stringstream. Here is my code:


    Code:
    fstream hashstream;
    stringstream buffer;
    hashstream.open("primaryfile.txt");
    hashstream.read (buffer,50);
    somestruct = parseLine(buffer)
    
    
    mystruct parseLine(stringstream rawline)
    {
    	int DEL;
    	string SSN;
    	long int PTR;
    	
    	rawline.seekg(0, ios::beg);
    	rawline>>DEL;
    	rawline.ignore();
    	rawline>>SSN;
    	for (i=0;i<31;i++)
    		rawline.ignore();
    	rawline>>PTR;
    	return mystruct; //contains the 3 variables. edited function for clarity
    }
    Note: It goes int, 3 strings, long int. the first/last name are padded to 10 and 20 chars respectively (with blank spaces) and all vars are separated with one blank space.

    What if I want to write to the file using a function?

    Reading from it is easy since I just copy to a buffer and pass that. Writing using a function would require opening/closing a file stream which might need to be different than the one in int main().

    What's the best way to handle it?
    Any way to do it easily with pointers? without having to pass stuff back and forth?

    Do I still use fstream without arguments to read and write?

    Thanks for your time.
    Last edited by gozu; 08-07-2007 at 01:35 PM.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    I am a bit confused what your question is. Do you want to open a stream in a function that main doesn't need to know about, or do you want a function to act upon a stream in which was created in main?

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    Quote Originally Posted by prog-bman View Post
    I am a bit confused what your question is. Do you want to open a stream in a function that main doesn't need to know about, or do you want a function to act upon a stream in which was created in main?
    The second one. The function would act upon a stream created in main (from the primaryfile.txt)

    I'd also like to clean up my main by doing the actual writing to that same file in another function but I don't know what the best way to do that is.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    streams are typically passed by reference (as far as I know, they're not copyable - though stringstreams may be an exception to this). Your function signature probably wants to look something like this
    Code:
    mystruct parseLine( stringstream& rawline )
    {
       // ...
    }
    This would also work if you were to substitute stringstream for any other type from the stream libraries

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> This would also work if you were to substitute stringstream for any other type from the stream libraries

    std::istream& might be best, so that any input stream could be used.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM