473,505 Members | 16,800 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to handle missing values in C++ using strtok function?

151 New Member
Hi,

I am parsing one tab delimited file with some missing values. When i use the strtok function, I can not get the values based on index in vector. The code shown below explains my pblm.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <string.h>
  3. #include <vector.h>
  4.  
  5. using namespace std;
  6.  
  7. int main ()
  8. {
  9.   char str[] ="XYZ 2 a sample      data";
  10.   char * pch;
  11.   vector <char*> vec;
  12.   pch = strtok (str,"\t");
  13.   while (pch != NULL)
  14.   {
  15.     vec.push_back(pch);
  16.     pch = strtok (NULL, "\t");
  17.   }
  18.   cout << "Fifth null element is "<<vec[4]<<"\n";
  19.   return 0;
  20. }
In the above code i am supposed to get null value (missing) but i am getting "data" as output. How can i handle missing values?
Thansk in advance.
Sep 9 '11 #1
1 2682
Banfa
9,065 Recognized Expert Moderator Expert
Look at your string, "XYZ 2 a sample data", it has 5 tokens
  1. XYZ
  2. 2
  3. a
  4. sample
  5. data
So of course vec[4] wont be NULL. Worst than that, since you don't set the size of your vector if it had less than 5 strings vec[4] wouldn't be NULL, it would be an out of bounds memory access, or if you used vec.at(4) which checks index boundaries you would get an exception.

I can begin to describe how bad a function strtok is. You should avoid using it at all costs. Get used to doing something else because if you ever program in a multi-threaded environment you will need to avoid it because it is not at all re-entrant or thread safe.

A std::istringstream should work just as well.

As to your problem, since you have not said what data format you are expecting in your string and since you do not have a designed way to read that format it is hard to give further advice.
Sep 9 '11 #2

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

Similar topics

2
3532
by: Duncan Smith | last post by:
Hello, I'm not very experienced in SQL and I need some advice. I have a comma separarated values file containing around 20 million records and about 20 fields. There are many missing values...
5
3851
by: krini_pop | last post by:
I have a string that is delimited by commas. I'm using strtok and putting the values in a vector. In some cases, I may have 2 commas side by side and therefore need it to insert a null value. Right...
1
1478
by: Paul Brun | last post by:
Hello all, Well....I debugged in application further (see int32 to native int thread) and decided to make a new post as it is easier for trackability... Anyway, I am getting a Null Reference...
5
1708
by: AK | last post by:
I'm writing a Windows Forms application in C++.NET. I've defined function F which takes 2 arguments a & b. I need to use the pointer of F inside another function G & I can't figure out how to do...
20
17177
by: bubunia2000 | last post by:
Hi all, I heard that strtok is not thread safe. So I want to write a sample program which will tokenize string without using strtok. Can I get a sample source code for the same. For exp:...
7
2187
PEB
by: PEB | last post by:
When i'm trying to pass missing values to a function called from a query like: Expr1:my_function("fdf",,"fdf",,,,"fd") I always obtain an error! I'll try using: ...
4
3831
by: prashant252 | last post by:
i'm using strtok function to read a file.... but it does not give correct output if the delimiter is without any value in between.. example ~ is my delimiter.. and text file line is-...
4
4744
by: ohaqqi | last post by:
Hi everybody. I haven't programmed anything in about 8 years, I've read up a little bit on C and need to write a shell in C. I want to use strtok() to take an input from a user and parse it into the...
2
4038
by: harius | last post by:
I'm looking to load data in an excel sheet from a database. I would like to do this via a function, so that when I recalc the sheet, the data is loaded. The function is something like: ...
3
4610
by: rola248 | last post by:
hi all, if we use strtok toseparate a line into two parts,say rola : 23 for(i=0;i<index;i++){ ptr=strtok(line,":"); while(ptr!=NULL) { strcpy(num,ptr); ptr=strtok(NULL,":"); }
0
7218
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
7103
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...
1
7021
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
7478
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
5614
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,...
1
5035
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...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
409
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.