Results 1 to 7 of 7

Thread: strtok and '\r\n'

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default strtok and '\r\n'

    Qt Code:
    1. char delims = ',';
    2. char* result = NULL;
    3. result = strtok( _buffer, delims );
    4. vector<double> v;
    5. while (result != NULL) { //and !endOfLine
    6. result = strtok( NULL, delims );
    7. v.push_back( atof(result) );
    8. }
    9. InsertInVectorOfVector(v)
    To copy to clipboard, switch view to plain text mode 
    Hello,
    I have to read the _buffer into a a vector; problem is that I need also to check when I step over the '\r\n', because in this case I need to insert 'v' in a vector of vector; Is there a way to do that control using strtok() ?

    Thanks,
    Regards

  2. #2
    Join Date
    Jul 2008
    Location
    Netherlands
    Posts
    33
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: strtok and '\r\n'

    You could use a copy of buffer and use strtok(_buffer, '\r') on that and compare the pointers. But if _buffer is very large, that's not really an option.

    My suggestion would be to use QByteArray and its members QByteArray::indexOf() and QByteArray::left() and optionally QByteArray::simplified() to get the data from your buffer.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: strtok and '\r\n'

    Quote Originally Posted by lvi View Post
    You could use a copy of buffer and use strtok(_buffer, '\r') on that and compare the pointers. But if _buffer is very large, that's not really an option.
    My suggestion would be to use QByteArray and its members QByteArray::indexOf() and QByteArray::left() and optionally QByteArray::simplified() to get the data from your buffer.
    I can't use qt library; only the c++ standard library
    Regards

  4. #4
    Join Date
    Jul 2008
    Location
    Netherlands
    Posts
    33
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: strtok and '\r\n'

    In that case...
    I would first tokenize the buffer by strtok(_buffer, '\n') and then tokenize by strtok(_buffer, ','):
    Qt Code:
    1. char* line = strtok(_buffer, '\n');
    2. while (line) {
    3. char* c = strtok(_buffer, ',');
    4. while (c) {
    5. double number = atof(c);
    6. c = strtok(NULL, ',');
    7. }
    8. line = strtok(NULL, '\n');
    9. }
    To copy to clipboard, switch view to plain text mode 

    Or you could use std::string::first_index_of() and std::string::substr() to achieve the same:
    Qt Code:
    1. int lineStart = buffer.first_index_of('\n');
    2. int lineEnd = buffer.first_index_of('\n', lineStart + 1);
    3. std::string line = buffer.substr(lineStart, lineEnd - lineStart);
    To copy to clipboard, switch view to plain text mode 
    In some loop... str::first_index_of returns std::npos if the delimeter is not found.

    Since your lines are terminated by \r\n, you need to use do something smart with the pointers/indices (e.g. increment by 1 appropriately).

    Use code at own risk ;-)

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: strtok and '\r\n'

    Hello,
    IMHO there's an error in the first version of your code ( the second strtok); this is my version of code (probably is what you wanted); anyway it doens't work because at //*, line takes "nullptr" and this first loop end...any hints?
    Qt Code:
    1. vector<double> v;
    2. vector< vector<double> > doubleVector;
    3. char* line;
    4. line = strtok(_buffer, "\r\n");
    5. while ( line != NULL ) {
    6. cout << line << endl;
    7. char* value = strtok(line, ",");
    8. while ( value != NULL) {
    9. cout << value << endl;
    10. v.push_back( strtod(value, NULL) );
    11. value = strtok(NULL, ",");
    12. }
    13. doubleVector.push_back(v);
    14. v.clear();
    15. line = strtok(NULL, "\r\n"); //* <------ problem
    16. }
    To copy to clipboard, switch view to plain text mode 
    Regards

  6. #6
    Join Date
    Jul 2008
    Location
    Netherlands
    Posts
    33
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: strtok and '\r\n'

    You are right about the second strtok. But my code is flawed anyway

    My best guess would be that on line #15 the buffer has no newlines left:

    Qt Code:
    1. // buffer: 0,1,2,3,4\r\n5,6,7,8,9\r\n
    2. line = strtok(buffer, "\r\n"); // buffer: 0,1,2,3,4\0\n5,6,7,8,9\r\n
    3. value = strtok(line, ','); // buffer: 0\01,2,3,4\0\n5,6,7,8,9\r\n
    4. while ( value != NULL ) {
    5. value = strtok(NULL, ',');
    6. }
    7. // at this point, buffer: 0\01\02\03\04\0\n5,6,7,8,9\r\n
    To copy to clipboard, switch view to plain text mode 
    You'd need to fiddle around with some pointers to get past the \0. But then again you need to be careful not to get a segfault at the end of your buffer or something.
    I suggest you use std::string, it will save you tons of trouble

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: strtok and '\r\n'

    hello,
    I'm trying to work with this code but it doens't work with ','; any helps, please?
    Qt Code:
    1. istringstream is("100,200,300", istringstream::in);
    2. int num;
    3. vector<int> vec;
    4. is.exceptions(~ios::goodbit);
    5. try {
    6. while ( is.good() ) {
    7. is >> num;
    8. vec.push_back(num);
    9. if (is.peek() == ',')
    10. is.ignore(1);
    11. }
    12. }
    13. catch(ios_base::failure failure) {
    14. assert (!is.good());
    15. if (!is.eof()) {
    16. is.clear();
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    It works fine only with a space between the numbers (but even in this case, at the end of scanning, an exception is thrown
    Regards

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.