473,398 Members | 2,188 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,398 software developers and data experts.

Using strcmp() after using strtok()

ern
//I used strtok() like this:

fgets(userCommand, MAX_COMMAND_SIZE, stdin);
g_UserCommands[0] = strtok(command, " ");
while(g_UserCommands[i] != NULL){
i+=1;
g_UserCommands[i] = strtok(NULL, " ");
}

//Now I want to use strcmp() like this:

if(strcmp(g_UserCommands[0], "script")==0){
return (executeScript(arg1));
}

/* Unfortuantely, I'm not getting desired results, probably because
the strings in g_UserCommands are not null terminated. Any suggestions
on a better way to get null terminated strings in g_UserCommands or
suggestions in general? I was hoping for a function that does what
strtok() does, but also providing null termination.

Thanks!
*/

Nov 15 '05 #1
3 2889

ern wrote:
//I used strtok() like this:

fgets(userCommand, MAX_COMMAND_SIZE, stdin);
g_UserCommands[0] = strtok(command, " ");
while(g_UserCommands[i] != NULL){
i+=1;
g_UserCommands[i] = strtok(NULL, " ");
}

//Now I want to use strcmp() like this:

if(strcmp(g_UserCommands[0], "script")==0){
return (executeScript(arg1));
}

/* Unfortuantely, I'm not getting desired results, probably because
the strings in g_UserCommands are not null terminated. Any suggestions
on a better way to get null terminated strings in g_UserCommands or
suggestions in general? I was hoping for a function that does what
strtok() does, but also providing null termination.

Thanks!
*/


Is that the real code? If so, you are reading into the
userCommand variable and strtoking the command variable. Can
you show a complete small compilable program that shows
your problem?

strtok does terminate the strings, that is not your problem.

-David

Nov 15 '05 #2
Replace below by a strcpy between source and destination and then you
will see the intended results

g_UserCommands[i] = strtok(NULL, " ");

-Ganesh

Nov 15 '05 #3
ga**********@gmail.com wrote:
Replace below by a strcpy between source and destination and then you
will see the intended results

g_UserCommands[i] = strtok(NULL, " ");

See sig below
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Nov 15 '05 #4

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

Similar topics

4
by: collinm | last post by:
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="_";
20
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:...
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...
14
by: spibou | last post by:
What is strcmp supposed to return if one or both arguments passed to it are NULL ?
14
by: Mr John FO Evans | last post by:
I cam across an interesting limitation to the use of strtok. I have two strings on which I want strtok to operate. However since strtok has only one memory of the residual string I must complete...
4
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...
1
by: ohaqqi | last post by:
Hi guys, I'm still working on my shell. I'm trying to implement a function typefile that will take a command line input as follows: > type <file1> This command will implement a catenation of...
7
by: Neel | last post by:
Hi friends, 'm getting weird output with strcmp function. 'm unable to detect the error. code is: string myline=""; line=myline.c_str(); line='\0';
4
by: spiralfire | last post by:
I wrote a translator, that reads a DIMACS graph format and writes to a simpler format... basically DIMACS format is: c comment p type nodes edges //type is alwats edge on my problems,...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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.