我以一个txt文件作为输入,并且我的第一个解析按预期工作,但是我很难得到第二个解析的逻辑。解析一个txt文件并写入两个单独的文件
例如:让我们说txt文件看起来是这样的:
.text
main:
... // some instruction (assembly)
exit:
... // exit instructions
.data
...
我试图做的是保存的.text后一切一个文件(的text.txt,让我们说),一切之后.data到另一个文件(data.txt)。我的第一个解析工作的方式是将所有内容复制到text.txt中,然后在strcmp(str,“.data \ n”)== 0时发生。此操作符合预期,但我不确定如何获取下半场。
起初我试图改变我的if(strcmp(str,“.data \ n”)== 0)从休息到一阵子的身体希望我可以继续使用文件*文件指针,但是,这最终没有完成执行,我必须退出我的终端。
感谢您的帮助,并让我知道是否需要发布其他内容。
void preprocessTextFile(FILE* file, FILE* file2, FILE* file3) {
char str[256];
while(!feof(file)){
fgets(str, sizeof(str), file);
if(strcmp(str, ".data\n") == 0){
preprocessDataFile(file, file3);
}
...
}
void preprocessDataFile(FILE* file, FILE* file2) {
char str[256];
while(!feof(file)){
// gets rid of ".data"
char* word = strchr(str, '.');
if(word){
*word = 0;
}
// gets rid of comments
char* hashCom = strchr(str, '#');
if(hashCom){
*hashCom = 0;
}
char* word2 = strtok(str, ", \t\n");
int count = 0;
while(word2){
fprintf(file2, "%s%s", count++ ? " " : "", word2);
word2 = strtok(NULL, ", \t\n");
}
if(count){
fprintf(file2, "\n");
}
}
}
这会导致一个永远不会在我的终端结束的exe文件。我的策略是一旦它达到.data它调用preprocessDataFile()使用前一个函数的指针(我假设它仍然会指向.data传递到preprocessDataFile时),然后我可以复制它的其余部分使用同样的逻辑到第二个文件(data.txt)
有趣,我想知道为什么它不适合我 – CSDUDE