[RESOLVED] problem passing delimiter to getline as a variable
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] problem passing delimiter to getline as a variable

  1. #1
    Join Date
    May 2009
    Location
    Boston
    Posts
    312

    [RESOLVED] problem passing delimiter to getline as a variable

    Hello,

    I am parsing some text data and have a simple function that reads in each line to a string, passes the string to a stringstream, parses the stringstream on a delimiter, and then loads the parsed elements into a vector.

    Code:
    // string to hold each line of input file
    string new_line, delimiter;
    // for parsing row into cells
    string cell_value, temp_string;
    // to parse row into vector
    stringstream row_stream;
    // assign tab as delimiter, could be space or comma
    delimiter.assign('\t');
    
    // read input file
    while(getline(inputfile_istream, new_line)) {
    
       // remove DOA '\r' EOL chars
       new_input_line.erase (remove(new_line.begin(),new_line.end(),'\r') , new_line.end());
    
       // add row of data to stringstream
       row_stream << new_line;
    
       // parse stringstream on tab and push into vector in row data object
       while(getline(row_stream, cell_value, delimiter.c_str() )) { row_data.push_back(cell_value); }
    
       // flush buffer
       row_stream.str(""); row_stream.clear();
    
       // clear line for next row
       new_line.clear();  new_line.assign(""); row_data.clear();
    
    }
    The cod above obviously doesn't do anything as each row is deleted after being red in. The problem I am having is in passing the delimiter to getline. I would like the code to be general so I can pass any delimiter.

    I know that getline accepts the delimiter as a character,

    Code:
    istream& getline (istream& is, string& str, char delim);
    so I thought that calling c_str() on the string would return the character that I need. I am getting an error,

    Code:
    error: no matching function for call to ‘getline(std::stringstream&, std::string&, const char*)’
    I could declare a character for the delimiter instead of the string but I'm not sure how to assign a tab \t to a single character. I could also do something kludgy like having multiple getline statements with hard coded delimiters, but that is not elegant and not universally applicable to any delimiter.

    I'm surprised that getline uses a character for the delimiter since a string would allow for multi character delimiters which would be useful in some cases.

    Any suggestions on how to fix this?

    LMHmedchem

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,588

    Re: problem passing delimiter to getline as a variable

    Code:
    while(getline(row_stream, cell_value, delimiter.c_str() )) { row_data.push_back(cell_value); }
    Try
    Code:
    while(getline(row_stream, cell_value, delimiter[0] )) { row_data.push_back(cell_value); }
    Also
    Code:
    const char delim = '\t';
    ...
    while(getline(row_stream, cell_value, delim )) { row_data.push_back(cell_value); }
    calling c_str() on the string would return the character that I need
    c_str() returns a pointer to a c-style null-terminated string (type const char*) - not a type char.
    Last edited by 2kaud; April 10th, 2017 at 01:06 PM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  3. #3
    Join Date
    May 2009
    Location
    Boston
    Posts
    312

    Re: problem passing delimiter to getline as a variable

    Quote Originally Posted by 2kaud View Post
    Code:
    const char delim = '\t';
    while(getline(row_stream, cell_value, delim )) { row_data.push_back(cell_value); }
    Thanks, I went with this. I just wasn't sure I could assign \t to a single character since it looks like two characters. Thanks for reminding me to use const as well, I never seem to remember that.

    LMHmedchem

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,588

    Re: problem passing delimiter to getline as a variable

    A char sequence starting with \ is treated as a single char. See https://msdn.microsoft.com/en-us/library/6aw8xdf2.aspx for a list of those supported. As well as the usual \t, \n etc you can also specify the value of a char in hex and octal.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)