Error while tokenizing strings from a string
I want to generate a symbol table from a SIC Assembly code. For that I have tried to separate every strings form the assembly code. Hence while attempting first step for that, I tried this code.
Here, What I have tried is to read the assembly code form a file line by line. And then to separate the strings in the line to tokens.
When I remove the token separation section (as mentioned in the code) I am getting all lines printed as expected. But If I run along with token separation, the first line is getting read and the tokens are separated as I expected. But for the second line I am getting error as segmentation fault. I couldn't trace where I went wrong.
So, I need explanations form experts.
Thanks in advance.
FILE* sourceCode = fopen("/home/muthu/LangFiles/SIC/PASS1/PROGRAM.txt","r"); if(checkForFileOpeningErrors() == ERROR) //Iam using separate function return EXIT_FAILURE; //Terminate the program. int maxLineLength = 50; while(1) { char* lineReader = NULL; // since getline will reallocate. if( getline(&lineReader,(size_t*)&maxLineLength,sourceCode) == -1 ) break; printf("%s",lineReader); // TOKEN SEPARATION STARTS HERE.... If I comment this section out iam getting all lines printed char* wordReader; wordReader = strtok(lineReader," \n"); 开发者_如何学Python printf("%s\n",wordReader); while(1) { wordReader = strtok(NULL," \n"); printf("%s\n",wordReader); } // TOKEN SEPARATION ENDS HERE.... }
My FILE:
COPY START 1000
FIRST STL RETADR
CLOOP JSUB RDREC
LDA LENGTH
COMP ZERO
JEQ ENDFIL
JSUB WRREC
J CLOOP
.
.
.
END
My Sample output:
muthu@muthu-G31M-ES2L:~/LangFiles/PASS1$ ./a.out
All Files successfully opened!! Operation has begun...
COPY START 1000
COPY
START
1000
segmentation Fault.
When do you expect this loop to terminate?
while(1)
{
wordReader = strtok(NULL," \n");
printf("%s\n",wordReader);
}
Uh, you're using strtok()
wrong. It will return NULL
when no more tokens are found, and you shouldn't try to print that. Also, your loop is never-ending, which will be a problem.
The loop should probably look something like this, since as-is now, you pass the same argument to strtok()
on every iteration, which will of course never succeed:
for(wordReader = strtok(lineReader, " \n");
wordReader != NULL;
wordReader = strtok(NULL, " \n"))
{
printf("found token: '%s'\n", wordReader);
}
You never break out of the while(1)
loop, so it tries to go on forever, but causes a segmentation fault when strtok
returns NULL
.
You need to check whether strtok
returns NULL
, and break out of the loop at that point.
精彩评论