我想从 SIC 汇编代码生成符号表。为此,我试图将每个字符串从汇编代码中分离出来。 因此,在尝试为此迈出第一步时,我尝试了这段代码。
在这里, 我尝试过的是逐行读取文件中的汇编代码。然后将行中的字符串分隔为标记。
当我删除标记分隔部分(如代码中所述)时,我将按预期打印所有行。 但是如果我按照 token 分离运行,第一行就会被读取并且 token 会按照我的预期分开。但是对于第二行,我收到的错误是段错误。 我无法追踪哪里出错了。
所以,我需要专家的解释。
提前致谢。
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"); printf("%s\n",wordReader); while(1) { wordReader = strtok(NULL," \n"); printf("%s\n",wordReader); } // TOKEN SEPARATION ENDS HERE.... }
我的文件:
COPY START 1000
FIRST STL RETADR
CLOOP JSUB RDREC
LDA LENGTH
COMP ZERO
JEQ ENDFIL
JSUB WRREC
J CLOOP
.
.
.
END
我的示例输出:
muthu@muthu-G31M-ES2L:~/LangFiles/PASS1$ ./a.out
All Files successfully opened!! Operation has begun...
COPY START 1000
COPY
START
1000
segmentation Fault.
最佳答案
你预计这个循环什么时候结束?
while(1)
{
wordReader = strtok(NULL," \n");
printf("%s\n",wordReader);
}
关于c - 从字符串中标记字符串时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6663938/