Reputation: 117
#define DELIMS "!\"#$%&()|'*+,?/:;<=>@[\092]^_{}~\177"
void getFileLine(FILE *fp)
{
char *word, *ptr;
int tokennum, count;
char buffer[100];
while(!feof(fp))
{
(fgets(buffer, 100, fp));
ptr = buffer;
for(tokennum = 1; word = strtok(ptr, DELIMS);ptr = NULL, tokennum++)
{
word = strtok(ptr, DELIMS);
printf("%s\n", word);
}
}
}
So I am passing in a file that has a sample program in it. My job is to remove some delims and pass each word from the code into a tree.
While I am not at the tree part and just working on getting the strings manipulated the way I want, I am having some issues.
So, as I read the lines from the .txt
file, I am getting part of what I want. The first couple of lines from the .txt
is as follows:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FLUSH while( getchar()!= '\n')
Now, after it runs through my code, it turns it into:
include
include
include
include
define FLUSH while
The words in "
and <>
are removed because those are a few of the delims.
The problem I am having is at the define FLUSH while
part. When a line as more than one word that is not a delim, I want each word to be displayed separately, making the output:
include
include
include
include
define
FLUSH
while
As you can see, the define FLUSH while
now has each word on a separate line.
I thought making ptr=NULL
would cause the strtok
to reuse the line until it reached the end, but again I am having a little trouble getting this done. Any advice/help would be great. Thanks.
Upvotes: 0
Views: 295
Reputation: 571
The issue is the way you have defined your for loop:
Here is a simplified snippet of the code:
for (; word = strtok(ptr, DELIMS);ptr = NULL)
{
word = strtok(ptr, DELIMS);
printf("%s\n", word);
}
What this is equivalent to is:
while(word = strtok(ptr, DELIMS))
{
word = strtok(ptr, DELIMS);
printf("%s\n", word);
ptr = NULL;
}
Notice how you call strtok
twice in each iteration, but only print once? This means you will lose every other token.
Furthermore, you haven't added (space) to your list of tokens, so it won't split on spaces.
Upvotes: 3