#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.