473,805 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strtok problem - strcmp

hi

this is my code to analyse a file

void analyzeFilename ()
{
char string[]="B_L2_HLD_GRN_ NOR_Run_Counter .txt";
char *tokenptr;
char *seperators="_" ;

tokenptr = strtok(string,s eperators); /* get the first token
*/
while (tokenptr != NULL) /* while more tokens in
strng */
{
printf("\t%s\n" ,tokenptr); /* print first token */
tokenptr = strtok(NULL,sep erators); /* get next token */

}
}
that work fine

now i want to compare the token string, i do:
void analyzeFilename ()
{
char string[]="B_L2_HLD_GRN_ NOR_Run_Counter .txt";
char *tokenptr;
char *seperators="_" ;

tokenptr = strtok(string,s eperators); /* get the first token
*/
while (tokenptr != NULL) /* while more tokens in
strng */
{
printf("\t%s\n" ,tokenptr); /* print first token */
tokenptr = strtok(NULL,sep erators); /* get next token */

if(strcmp(token ptr,"HLD")==0)
printf("hold\n" );
}
}

i get a segmentation fault

any idea?

Nov 14 '05 #1
4 3544


collinm wrote:
[...]
now i want to compare the token string, i do:
void analyzeFilename ()
{
char string[]="B_L2_HLD_GRN_ NOR_Run_Counter .txt";
char *tokenptr;
char *seperators="_" ;

tokenptr = strtok(string,s eperators); /* get the first token
*/
while (tokenptr != NULL) /* while more tokens in
strng */
{
printf("\t%s\n" ,tokenptr); /* print first token */
tokenptr = strtok(NULL,sep erators); /* get next token */

if(strcmp(token ptr,"HLD")==0)
printf("hold\n" );
}
}

i get a segmentation fault


Do the strcmp() *before* strtok().

--
Er*********@sun .com

Nov 14 '05 #2
collinm wrote:
hi

this is my code to analyse a file

void analyzeFilename ()
{
char string[]="B_L2_HLD_GRN_ NOR_Run_Counter .txt";
char *tokenptr;
char *seperators="_" ;

tokenptr = strtok(string,s eperators); /* get the first token
*/
while (tokenptr != NULL) /* while more tokens in
strng */
{
printf("\t%s\n" ,tokenptr); /* print first token */
tokenptr = strtok(NULL,sep erators); /* get next token */

}
}
that work fine

now i want to compare the token string, i do:
void analyzeFilename ()
{
char string[]="B_L2_HLD_GRN_ NOR_Run_Counter .txt";
char *tokenptr;
char *seperators="_" ;

tokenptr = strtok(string,s eperators); /* get the first token
*/
while (tokenptr != NULL) /* while more tokens in
strng */
{
printf("\t%s\n" ,tokenptr); /* print first token */
tokenptr = strtok(NULL,sep erators); /* get next token */
tokenptr will be NULL in the very last loop iteration at this
point; thus, using it after this line will give you undefined
behaviour (and in your case, luckily, a segfault).
Put the call to strtok() at the end of the loop.

I have not looked to closely at your code nor tested it.

--Michael
if(strcmp(token ptr,"HLD")==0)
printf("hold\n" );
}
}

i get a segmentation fault

any idea?

--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #3
collinm wrote:
void analyzeFilename ()
{
char string[]="B_L2_HLD_GRN_ NOR_Run_Counter .txt";
char *tokenptr;
char *seperators="_" ;

tokenptr = strtok(string,s eperators); /* get the first token
*/
while (tokenptr != NULL) /* while more tokens in
strng */
{
printf("\t%s\n" ,tokenptr); /* print first token */
tokenptr = strtok(NULL,sep erators); /* get next token */
Consider the condition that breaks your loop -- when strtok returns
NULL. But before that condition is reached, you use the returned value
without checking it. My guess is that it's returning NULL and then
you're passing the NULL to strcmp() which causes the segmentation fault.
if(strcmp(token ptr,"HLD")==0)
printf("hold\n" );
}
}

i get a segmentation fault

any idea?


hope that helps,
John
Nov 14 '05 #4
On 24 Mar 2005 09:22:36 -0800, in comp.lang.c , "collinm"
<co*****@laboit eaprog.com> wrote:
tokenptr = strtok(NULL,sep erators); /* get next token */
if strtok returned NULL here...
if(strcmp(token ptr,"HLD")==0)


.....this will segfault

you need to check for NULL before using the pointer
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2914
by: ern | last post by:
//I used strtok() like this: fgets(userCommand, MAX_COMMAND_SIZE, stdin); g_UserCommands = strtok(command, " "); while(g_UserCommands != NULL){ i+=1; g_UserCommands = strtok(NULL, " "); } //Now I want to use strcmp() like this:
13
4931
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 happen before 'splitCommands' entered the picture. The problem is in splitCommands( ) somehow modifying the pointer, but I HAVE to call that function. Is there a way to make a copy of it or something ? /* HERE IS MY CODE */ char *...
20
17247
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: 0.0.0.0--->I want to tokenize the string using delimiter as as dot. Regards
8
1932
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 dreams." #include <stdafx.h> #include <string.h> #include <stdio.h> int main()
4
2737
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 somehow confused or upset. I have the following code, which I am using to tokenise some input which is in th form x:y:1.2: int tokenize_input(Sale *sale, char *string){
3
3811
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 <string> #include <stdlib.h> using namespace std;
13
2413
LucasBishop
by: LucasBishop | last post by:
Hi everyone; i'm working with a program that can't split a strig of characters if i introduce out of the program. It only work's if the string is put it directly in the the soucer code, here it is: #include<stdio.h> #include<string.h> #include<conio.h> #include<ctype.h> #include<stdlib.h> int evaluar(char *simbolo)
29
2593
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>
11
17180
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
0
9718
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10613
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10368
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10107
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4327
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 we have to send another system
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.