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

strtok question

schmals
Hi,

I am writing a UNIX shell program and have encountered a problem that I have spent way too many hours trying to solve to no avail. I have narrowed down my problem concisely and made a easier to follow test program that has the same problem in it. Here is the code for the sample program:

Expand|Select|Wrap|Line Numbers
  1.     char string[] = "hello my name is Scott";
  2.     char* word;
  3.     char** sargv;
  4.     int arg_count = 0;
  5.     int i;
  6.  
  7.     /* get argument count */
  8.     word = strtok(string, " ");
  9.     while (word != NULL) {
  10.         word = strtok(NULL, " ");
  11.         arg_count++;
  12.     }
  13.  
  14.     /* allocate memory in heap for array of strings */
  15.     arg_count++; /* increment arg_count so that sargv will be null-terminating */
  16.     sargv = calloc(arg_count, sizeof(char*));
  17.  
  18.  
  19.     word = strtok(string, " "); /* get first word implicitly */
  20.     sargv[0] = malloc(strlen(word)*sizeof(char) + 1); /* allocate memory to hold it */
  21.     strcpy(sargv[0], word); /* add it to sargv */
  22.  
  23.     /* iterate through all arguments and add them to sargv */
  24.     for (i = 1; word != NULL; i++) {
  25.         printf("loop\n");
  26.         word = strtok(NULL, " ");
  27.         if (word != NULL) {
  28.             sargv[i] = malloc(strlen(word) * sizeof(char) + 1);
  29.             strcpy(sargv[0], word);
  30.         }
  31.     }
  32.  
  33.     /* print values of sargv */
  34.     for (i = 0; sargv[i] != NULL; i++) {
  35.         printf("sargv[%i]: %s\n", i, sargv[i]);
  36.     }
  37.  
  38.  
The problem happens when I get to the for loop that should populate sargv[] with the arguments (hello, my, name, is, Scott). It get the first argument because it's outside of the loop, but when it goes back to get the second argument (in word), it must be set to NULL instead of the argument and exits the loop. Why isn't strtok working for me here like it should?! Any help would be very, very appreciated!
Apr 23 '08 #1
1 1331
gpraghuram
1,275 Expert 1GB
Hi,
Since you have done a strtok the ip string wont have anything or only the first part of the string.(That is to get number of tokens)
You are doing strtok again to get the individual strings.
Since the source string is empty strtok is not working.
What i suggest is to get the number of tokens iterate through the string to find number of spaces.
Or after the first strtok reinitialize the input again.

Raghuram
Apr 24 '08 #2

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

Similar topics

5
by: tvn007 | last post by:
// I am using strtok to break the string // For example: to extract 5 from the string below: // TEST 1,P,5,PASS // Below is my code in C: ptr =strtok(testbuff,"...
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...
17
by: bofh1234 | last post by:
I need to delimit a string. The delimiters are a semicolon and comma. When I run the program I get a segmentation fault on the first strtok. I followed the examples of others and from my old C...
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...
8
by: cting76 | last post by:
If I understand it correctly, strtok scans from the first character after the separator. The code below: #include <iostream> #include <fstream> using namespace std; void main() { char...
5
by: Kelly B | last post by:
I need a function which returns me a "word" from a given string and then sets the pointer to the next one which is then retrieved during further calls to the function. I think strtok( ) is the...
11
by: Lothar Behrens | last post by:
Hi, I have selected strtok to be used in my string replacement function. But I lost the last token, if there is one. This string would be replaced select "name", "vorname", "userid",...
3
by: krista | last post by:
Hi, I am beginner of C++. When I try to read a file and put it into X and Y variables. I got that segmentation fault. the data file is data.txt: 4.4 6.8 3.2 -5.5 3.3 0.9
11
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
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
0
BarryA
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...
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:
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,...

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.