I hve to use NULL as the first arguement in strtok() after first call, i want to know that why i have to use NULL as arguement in place of actual string?
6 19105
I hve to use NULL as the first arguement in strtok() after first call, i want to know that why i have to use NULL as arguement in place of actual string?
Well, if you look at the strtok() documentation the first argument is the string you want to be parsed. If you are not passing it a string, you can't parse null for anything but null, and why are you even using strtok() to do nothing?
The first call you use a char array which has the elements you want parsed.
The second time you call it you pass it NULL as the first parameter to tell function to resume from the last spot in the string. Once the first call is made your char array receives the parsed string. If you don't put NULL you would lose your place and effectivly the last part of your string.
char * c_Ptr = NULL; //temp hold indivisual sections
char c_Ptr1[1000] = {NULL};
fgets(c_Ptr1, 1000, f_ptr) //Grabs a line from an open file
strtok(c_Ptr1, ","); //first one starts at the beginning
c_Ptr = strtok(NULL, ",");
The first call you use a char array which has the elements you want parsed.
The second time you call it you pass it NULL as the first parameter to tell function to resume from the last spot in the string. Once the first call is made your char array receives the parsed string. If you don't put NULL you would lose your place and effectivly the last part of your string.
char * c_Ptr = NULL; //temp hold indivisual sections
char c_Ptr1[1000] = {NULL};
fgets(c_Ptr1, 1000, f_ptr) //Grabs a line from an open file
strtok(c_Ptr1, ","); //first one starts at the beginning
c_Ptr = strtok(NULL, ",");
Ah, after the first call. Good catch, Studlyami.
Yes, good ol'strtok(). It keeps its position in a static variable. So if you use this thing in a multithreaded program you are on the road to a crash.
strtok() is OK for trivial programs or single-threaded programs. However, for real modern softweare programs, write a parser that doesn't use static variables.
What type of functions parse without using static variables? I've always used strtok to parse a file.
What type of functions parse without using static variables? I've always used strtok to parse a file.
You use a design pattern called State. There is an article on this in the C/C++ Articles forum. The examples are C++ but anything written in C++ can be written in C. It just takes a ton more work.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: faktujaa |
last post by:
Hi All,
I have used the available code snippet from microsoft for socket
communication.
But the following code throws an error mentioned above. Please check the
code and help me asap.
private...
|
by: Jörg Striegnitz |
last post by:
We apologize if you have received this CfP multiple times.
--------------------------------------------------------------------
SECOND CALL FOR CONTRIBUTIONS AND PARTICIPATION
Workshop on...
|
by: jasonkester |
last post by:
Just a heads up for anybody that comes across this in the future.
Noticed a strange behavior in RegExp.test() today. Check out the
following code. It will alternately display "chokes" and null,...
|
by: --CELKO-- |
last post by:
I am working on the third edition of SQL FOR SMARTIES. If anyone has
an SQL programming technique, trick or tip that they think should be in
the book, drop me an email.
You get a virtual beer...
|
by: Eliyahu Goldin |
last post by:
I am looking for an ActiveX control for viewing document images. I need only
basic viewing functions like zooming, rotating, scaling. Don't need any
editing, PhotoShop-style effects, printing,...
|
by: Paul |
last post by:
Hello,
The following code generates an exception error the *second* time it is
called.
Can anyone see what I'm doing wrong?
There is a similar example in Wrox Professional VB.NET Page 289.
...
|
by: Torsten Grust |
last post by:
Second Call for Papers
X I M E - P 2 0 0 6
3rd International Workshop on
XQuery Implementation, Experience and Perspectives
June 30, 2006
Collocated with ACM SIGMOD/PODS 2006, Chicago, USA...
|
by: es_ |
last post by:
Hi,
On other javascript group I didn't get answer. Maybe You'll know...
There's a strange problem with variables. It feels that they work only
after second call. It looks like this:
jsa="1"...
|
by: Fortis Florin |
last post by:
2nd CFP: Workshop on Workflow and Process Management (WfPM'08),
September 2008, Timisoara, Romania
EXTENDED DEADLINES...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |