473,804 Members | 2,119 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strtok stops to parse after encountering odd chars like - " /

2 New Member
Hi,

I have a text file as input which has odd chars like: - // "
Bellow is a sample:
Jul 6 00:00:13 proxyhost httpd[7266]: [ID 477494 local3.info] 172.26.74.212 - [06/Jul/2007:00:00:13 -0400] "GET http://www.google.com/gwt/i?i=08
9E3394C_C235D1F F_FA6C3045 HTTP/1.1" 200 200 383 78881e-1 N COMPLETE 158 REQ:220:0:130:4 :100:0:10:8:30: 0:60:0:5:0:200: 0 EXE:80:0:125:0: 15:0:230:0 MOD
:240:0:210:0:90 :0:110:0:20:0 RES:40:0:70:30: 120:0:260:0 PD:126:0 113 - 1

I read the file line by line and split it with strtok but once I try to split the odd chars strtok is not doing its job. Bellow is the C code I use:

/* Counter */
int i=0;

/* Read the file until EOF */
while ( fgets ( line, sizeof line, input_file_poin ter ) != NULL ) /* read a line */
{
pch = strtok (line, " ");
arra[i]= pch;
while (pch != NULL)
{
pch = strtok (NULL, " ");
arra[i]= pch;
i++;
}

}
printf("(%s) (%s) (%s) (%s) (%s) (%s) (%s) (%s) (%s) (%s) (%s) (%s) (%s) (%s) (%s)\n", arra[0],arra[1],arra[2],arra[3],arra[4],arra[5],arra[
6],arra[7],arra[8],arra[9],arra[10],arra[11],arra[12],arra[13],arra[14]);

Because of the char "-" strtok stops to process after it. If I put in the delimitors also the char "-" if get further with processing but break again at """ and also again at "/" from http://www.google.com.

The output I get with only space as delimitor:
(6) (00:00:18) (to5magproxy1) (httpd[7141]:) ([ID) (477494) (local3.info]) (172.30.128.215 ) () () () () (T) (1) (TE)

The problems are:
-The 1st field stored in arra[0] (should contain Jul) is missing:
-Once strok hits the char - it stops to parse.

There is a way to overcome this strtok behaviour and force it to parse regardles of the char types?
Jul 9 '07 #1
3 2515
gpraghuram
1,275 Recognized Expert Top Contributor
Hi,

I have a text file as input which has odd chars like: - // "
Bellow is a sample:
Jul 6 00:00:13 proxyhost httpd[7266]: [ID 477494 local3.info] 172.26.74.212 - [06/Jul/2007:00:00:13 -0400] "GET http://www.google.com/gwt/i?i=08
9E3394C_C235D1F F_FA6C3045 HTTP/1.1" 200 200 383 78881e-1 N COMPLETE 158 REQ:220:0:130:4 :100:0:10:8:30: 0:60:0:5:0:200: 0 EXE:80:0:125:0: 15:0:230:0 MOD
:240:0:210:0:90 :0:110:0:20:0 RES:40:0:70:30: 120:0:260:0 PD:126:0 113 - 1

I read the file line by line and split it with strtok but once I try to split the odd chars strtok is not doing its job. Bellow is the C code I use:

/* Counter */
int i=0;

/* Read the file until EOF */
while ( fgets ( line, sizeof line, input_file_poin ter ) != NULL ) /* read a line */
{
pch = strtok (line, " ");
arra[i]= pch;
while (pch != NULL)
{
pch = strtok (NULL, " ");
arra[i]= pch;
i++;
}

}
printf("(%s) (%s) (%s) (%s) (%s) (%s) (%s) (%s) (%s) (%s) (%s) (%s) (%s) (%s) (%s)\n", arra[0],arra[1],arra[2],arra[3],arra[4],arra[5],arra[
6],arra[7],arra[8],arra[9],arra[10],arra[11],arra[12],arra[13],arra[14]);

Because of the char "-" strtok stops to process after it. If I put in the delimitors also the char "-" if get further with processing but break again at """ and also again at "/" from http://www.google.com.

The output I get with only space as delimitor:
(6) (00:00:18) (to5magproxy1) (httpd[7141]:) ([ID) (477494) (local3.info]) (172.30.128.215 ) () () () () (T) (1) (TE)

The problems are:
-The 1st field stored in arra[0] (should contain Jul) is missing:
-Once strok hits the char - it stops to parse.

There is a way to overcome this strtok behaviour and force it to parse regardles of the char types?
Hi ,
First of all there is a problem in ur code.
Instead of copying the string you are assigning it to an arry which wont work.
Expand|Select|Wrap|Line Numbers
  1. arra[i]= pch; //this is wrong
  2. strcpy(arra[i],pch); ///use this
  3.  
You can pass multiple characters to strtok like strtok("str," -")
But if u dont want to tokenize using other characters then before calling strtok write your own function to remove those special characters

Raghuram
Jul 10 '07 #2
adolghiu
2 New Member
Hi,

As advised I put strcpy(arra[i],pch); instead of arra[i]= pch; but now I get "Segmentati on Fault - core dumped" when the program run this statement.
The compile is passing with no warnings/errors. Here is the new code:
Expand|Select|Wrap|Line Numbers
  1. ...
  2.         /* --- Initialize the variables --- */
  3.         int i=0;
  4.  
  5.         /* variable to read in the words from the file */
  6.         char line[3000]; /* no line above 3000 characters */
  7.         char *pch;
  8.  
  9.         /* Array to store the input line */
  10.         char *arra[3000];
  11. ...
  12.         /* Read the file until EOF */
  13.         while ( fgets ( line, sizeof line, input_file_pointer ) != NULL ) /* read a line */
  14.         {
  15.           pch = strtok (line, " ");
  16.           //arra[i]= pch;
  17.           strcpy(arra[i],pch);
  18.           return 0;
  19.           printf("(%s)\n",arra[0]);
  20.           return 0;
  21.           while (pch != NULL)
  22.           {
  23.             pch = strtok (NULL, " ");
  24.             //arra[i]= pch;
  25.             strcpy(arra[i],pch);
  26.             i++;
  27.           }
  28.  
Thx
Jul 10 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
As advised I put strcpy(arra[i],pch); instead of arra[i]= pch; but now I get "Segmentati on Fault - core dumped" when the program run this statement.
arra[i] is a pointer. You need to allocate memory for the strcpy() before you copy.

Expand|Select|Wrap|Line Numbers
  1. arra[i] = malloc(strlen(pch) +1);  //extra byte for the null terminator
  2.  
and then rememebr to delete this memory wne you are finished with it.
Jul 10 '07 #4

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

Similar topics

12
5622
by: BGP | last post by:
I am working on a WIN32 API app using devc++4992 that will accept Dow Jones/NASDAQ/etc. stock prices as input, parse them, and do things with it. The user can just cut and paste back prices into a window and hit a button to process it. The information thus enters the program as a char array. Prices can be between $1 and $100, including cents. So we can have prices such as 3.01, 1.56, 11.57, etc. The char array is an alphanumeric...
2
414
by: Ram Laxman | last post by:
Hi all, I have written the following code: /* strtok example */ #include <stdio.h> #include <string.h> static const char * const resultFileName = "param.txt";
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
7
4370
by: Peter | last post by:
hi all, the strtok() cannot phrase the token within another token, am i correct? For example, i want to get the second word of every row of a file, how to use strok to complete this? thanks from Peter (cmk128@hotmail.com)
2
2093
by: manochavishal | last post by:
Hi I am writing a Program in which i get input as #C1012,S,A#C1013,S,U I want to get C1012,S,A using strtok and then pass this to function CreateCopies which will further strtok this (C1012,S,A) and store the required
8
4513
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", "passwort" from "users" order by "users"
4
4777
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 command and its arguments. for example: copy <file1> <file2> will copy file 2 to file 1, del <file1> will delete a file, etc. The exit command is all I've implemented right now, but even that produces an error when executed...I'm sure I've got a...
75
25148
by: siddhu | last post by:
Dear experts, As I know strtok_r is re-entrant version of strtok. strtok_r() is called with s1(lets say) as its first parameter. Remaining tokens from s1 are obtained by calling strtok_r() with a null pointer for the first parameter. My confusion is that this behavior is same as strtok. So I assume strtok_r must also be using any function static variable to keep the information about s1. If this is the case then how strtok_r is re-...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
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
10354
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
10097
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
9175
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...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4313
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
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.