Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Strtok() command.

Status
Not open for further replies.

TheButcher

Programmer
Jul 6, 2001
11
US
Be prepared......I'm a newbie.

I have a problem. I have a file I have to read in and parse it depending upon white space.

Ex) #comment

I read this line in using fgets() into a string array. I then use an output pointer to do a string tokenizer.

After the eliminating the frontal white space(' ', \t, \n, \r), I want to check the 1st line to see if it is a comment. I can do this with an array(use array[0] to see if is '#'). But strtok() can only be done with a pointer. How do I check the 1st character of a pointer?

Maybe there is a better way to do this, but here is what I got.

while((!feof(FreadIn)) && (!(FileLine[0] == '.')))
{
fgets(FileLine, 100, FreadIn);
if ((FileLine[0] == ' ') || (FileLine[0] == '\t') || (FileLine[0] == '\n'))
{
strcpy(temp1, FileLine);
output1 = strtok(temp1, whitespace);
fputs(output1, Fintermediate);
fputc(' ', Fintermediate);
while((output1 = strtok(NULL, whitespace)) != NULL)
{
fputs(output1, Fintermediate);
fputc(' ', Fintermediate);
}
fputc(newLine, Fintermediate);
}
else if(FileLine[0] == '#')
{
fputs(FileLine, Fintermediate);
}
else if((FileLine[0] == '.') && (FileLine[1] == 'P'))
{
fputs(FileLine, Fintermediate);
}

1st...if there is space, tokenize it.
2nd...if 1st char is #, write it to file
3rd...if it is ".PROGRAM" go on to next section (not shown)



Thanks in advance!
 
If I understand your intentions correctly, strtok() doesn't seem like it's the tool for the job.

Maybe something like this will do the trick:

#include <ctype.h>
#include <stdio.h>
#include <string.h>

#define MAX_LINE 100 /* or whatever's appropriate */
int
main(void)
{
FILE *fp=fopen(&quot;foo.txt&quot;,&quot;r&quot;);
char line[MAX_LINE+1];

if (!fp) {
perror(&quot;fopen&quot;);
} else {
char *ptr;
while (fgets(line,sizeof line,fp)) {
ptr=line;
/* Skip any leading white space */
while (isspace((unsigned char)*ptr)) {
++ptr;
}
if (*ptr=='#') {
/* it's a comment */
} else if (strcmp(ptr,&quot;.PROGRAM&quot;)==0) {
/* &quot;program&quot; */
} else {
/* regular line */
}
}
}
return 0;
}

BTW, an array of char can be passed to any function that expects a pointer to char. In a value context, an array becomes a pointer to its first element.
Russ
bobbitts@hotmail.com
 
Thank You Mr. Bobbiitts!!!

Okay, have a new problem.

i get file line,
FileLine = &quot;\t NAME\t BYTE\t $&quot;Your Name&quot;\t #comment

I read in this line and tokenize it by whitespace as I did above. I put the &quot;NAME&quot; and &quot;BYTE&quot; into a table. This is no problem.

Here is the problem. I need to check the $&quot; to see that it is a string($&quot;&quot;) and not a character($''). I tried using the string tokenizer with an array, but it gave me a core dump. So I am still using a pointer.

char FileLine[500];
char *output1;
char whitespace[] = &quot; \t\r\n&quot;;
output1 = strtok(temp1, whitespace);
while((output1 = strtok(NULL, whitespace)) != NULL)
{/*stuff in here*/}

output1 is a pointer, so I cant do:
output1[0] == '$' && output1[1] == '&quot;';

or can I?

Thanks again!
 
I think I misunderstood where you wanted to use an array with strtok(). For the return value of strtok() you must use a pointer to char, as you are doing above.

if (output1[0] == '$' && output1[1] == '&quot;')

This is perfectly fine. You can use array subscripting on pointers.

The above can be written as you've done, or you can do this:

if (*output1=='$' && *(output1+1)=='&quot;')

In fact, the array subscript notation above gets converted to this form automatically by the compiler due to the fact that arrays become pointers to their first element in a value context. It's even legal to write the expression like this:

if (0[output1]=='$' && 1[output1]=='&quot;')

This expression will become:

if (*(0+output1)=='$' && *(1+output1)=='&quot;')

Because addition is commutative, it makes no difference.

All in all, the way you have it is probably the easiest to read.
Russ
bobbitts@hotmail.com
 
Russ,
You have saved me from the dreaded 'F'. Thank you once again!
Butch
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top