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_C235D1FF_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_pointer ) != 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?
3 2476
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_C235D1FF_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_pointer ) != 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. -
arra[i]= pch; //this is wrong
-
strcpy(arra[i],pch); ///use this
-
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
Hi,
As advised I put strcpy(arra[i],pch); instead of arra[i]= pch; but now I get "Segmentation Fault - core dumped" when the program run this statement.
The compile is passing with no warnings/errors. Here is the new code: -
...
-
/* --- Initialize the variables --- */
-
int i=0;
-
-
/* variable to read in the words from the file */
-
char line[3000]; /* no line above 3000 characters */
-
char *pch;
-
-
/* Array to store the input line */
-
char *arra[3000];
-
...
-
/* Read the file until EOF */
-
while ( fgets ( line, sizeof line, input_file_pointer ) != NULL ) /* read a line */
-
{
-
pch = strtok (line, " ");
-
//arra[i]= pch;
-
strcpy(arra[i],pch);
-
return 0;
-
printf("(%s)\n",arra[0]);
-
return 0;
-
while (pch != NULL)
-
{
-
pch = strtok (NULL, " ");
-
//arra[i]= pch;
-
strcpy(arra[i],pch);
-
i++;
-
}
-
Thx
As advised I put strcpy(arra[i],pch); instead of arra[i]= pch; but now I get "Segmentation 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. -
arra[i] = malloc(strlen(pch) +1); //extra byte for the null terminator
-
and then rememebr to delete this memory wne you are finished with it.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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";
|
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...
|
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:...
|
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...
|
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...
|
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",...
|
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...
|
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...
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |