473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

strtok and newline

mickey0
142 100+
Hello,
I have to read the _buffer (buffer contains '\r\n' properly) 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() ?

Expand|Select|Wrap|Line Numbers
  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)
  10.  
Thanks,
Aug 5 '08 #1
3 12061
weaknessforcats
9,208 Expert Mod 8TB
There is seldom a reason fo mixing the C string library with C++.

In this case, is there a reason you can't use getline() to fill your buffer?

If you know the format of the buffer, you can insert it in a stringstream using << and then extract out the token using >>.
Aug 5 '08 #2
mickey0
142 100+
There is seldom a reason fo mixing the C string library with C++.
In this case, is there a reason you can't use getline() to fill your buffer?
If you know the format of the buffer, you can insert it in a stringstream using << and then extract out the token using >>.
Hello, there isn't any reason; I'd like explore strtok; I wrote this solutions and for me should be right...instead it doens't work:
Expand|Select|Wrap|Line Numbers
  1.     vector<double> v;
  2.     vector< vector<double> > doubleVector;
  3.  
  4.     char* line;
  5.     line = strtok(_buffer, "\r\n");
  6.     while ( line != NULL ) {
  7.         cout << line << endl;
  8.         char* value = strtok(line, ",");
  9.             while ( value != NULL) {
  10.                 cout << value << endl;
  11.                 v.push_back( strtod(value, NULL) );
  12.                 value = strtok(NULL, ",");            
  13.             }        
  14.         doubleVector.push_back(v);
  15.         v.clear();
  16.         line = strtok(NULL, "\r\n"); //1
  17.  
  18.     }
  19.  
Problem's at //1, "line" takes bad ptr.... why this? Any suggestions, please?
Aug 5 '08 #3
Banfa
9,065 Expert Mod 8TB
Hello, there isn't any reason; I'd like explore strtok;
strtok is not a very good function, and specifically it isn't re-entrant and you use it in a re-entrant manor.

In general I would say you should not use strtok in any program that you intend to keep the source of for more than 10 minutes. There are a lot better ways of parsing a string even in C.

I would assign strtok to the list of C standard library functions that you don't need to know about because you are never going to call (along with atoi, atof, ato... because why call these when the superior strtol, strtoul, strtod exist).
Aug 5 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Fernando Barsoba | last post by:
Hi, I'm using strtok() in the following way: void obtain_param(char *pmsg, CONF_PARAMS *cnf ) { char *s1, *s2; size_t msg_len; s1 = strtok (pmsg,":"); if (s1) {
13
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't...
8
by: hu | last post by:
hi, everybody! I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0. Program is simple, but mistake is confusing. First, the below code can get right outcome:"ello world, hello...
4
by: Michael | last post by:
Hi, I have a proble I don't understand when using strtok(). It seems that if I make a call to strtok(), then make a call to another function that also makes use of strtok(), the original call is...
3
by: nomad5000 | last post by:
Hi everybody! I'm having trouble using strtok to fill a matrix with int nrs. from a file. the code that is not working is the following: #include <iostream> #include <fstream> #include...
29
by: Pietro Cerutti | last post by:
Hello, here I have a strange problem with a real simple strtok example. The program is as follows: ### BEGIN STRTOK ### #include <string.h> #include <stdio.h>
6
by: Sheldon | last post by:
Hi Everyone, I have a file with a set of strings per line and each line ends with a '\n' and each string separated by a whitespace. After reading each line and using strtok() to split the string...
11
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
12
by: Pilcrow | last post by:
Here is a quick program, together with its output, that illustrates what I consider to be a deficiency of the standard function strtok from <string.h>: I am using C:\>gcc --version gcc (GCC)...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.