我正在做一个程序,我使用 Dev C++。运行我的程序时终止。在调试时它说“分段错误”。我不知道它是否会进入无限循环。我不知道问题是否while(!feof(program))
出fscanf(...)
在代码中。
任何人都可以帮助解决这个问题吗?请参阅下面的程序:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
fpos_t curDesStart,curDesEnd;
fpos_t curDesLocBackup,curDes;
char *label,*opc,*operands;
char *curMacroName;
FILE *namTab,*desTab,*temp,*program;
namTab=fopen("namTab.txt","rw");
desTab=fopen("desTab.txt","rw");
temp=fopen("temp.txt","w");
program=fopen("program.txt","r");
while(!feof(program))
{
fscanf(program,"%[^\n]%[^\n]%[^\n]",label,opc,operands);
if(!strcmp(label,"MACRO"))
{
fprintf(desTab,"%s%s%s\n",label,opc,operands);
fgetpos(desTab,&curDesStart);
strcpy(curMacroName,label);
while(!strcmp(opc,"MEND"))
{
fscanf(program,"%s%s%s",label,opc,operands);
}
fprintf(desTab,"%s%s%s\n",label,opc,operands);
fgetpos(desTab,&curDesEnd);
fprintf(namTab,"%s%ld%ld\n",curMacroName,curDesStart,curDesEnd);
}
else
{
while(!feof(namTab))
{
fgetpos(desTab,&curDesLocBackup);
fscanf(namTab,"%s%ld%ld",curMacroName,curDesStart,curDesEnd);
if(!strcmp(curMacroName,label))
{
fsetpos(desTab,&curDesStart);
fscanf(desTab,"%s%s%s\n",label,opc,operands);
fprintf(temp,".%s%s%s\n",label,opc,operands);
do{
fprintf(temp,"%s%s%s\n",label,opc,operands);
fgetpos(desTab,&curDes);
}while(curDes<=curDesEnd);
fsetpos(desTab,&curDesLocBackup);
fsetpos(namTab,0);
}
else
{
fprintf(temp,"%s%s%s\n",label,opc,operands);
}
}
}
}
fclose(program);
fclose(namTab);
fclose(desTab);
fclose(temp);
getch();
return 0;
}