如何使用fscanf()避免出现特定行?

我有一个包含sic机器的汇编语言的文件。 例如:

   LDA ALPHA
   ADD Beta
   ;This is a comment line.
   ;just an example code.
   END

在使用fscanf()阅读时如何避免使用注释行。请注意,行的长度不是固定的。注释行也可以出现在任何地方。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main(int argc,char *argv[])
{
    if(argc<2){
        printf("No input file.\nExecution terminated.\n");
        exit(0);
    }
    FILE *infile,*outfile;
    int locctr,start=0,i,optablen=6;
    char opcode[25],label[25],operand[25];
    char optab[6][5]={"LDA","STA","LDCH","STCH","ADD","SUB"};
    infile=fopen(argv[1],"r");
    outfile=fopen("SYMTAB.txt","w");
    fscanf(infile,"%s%s%s",label,opcode,operand);
    if(strcmp(opcode,"START")==0)
    {
        start=atoi(operand);
        locctr=start;
    }
    else
        locctr=start;
    fscanf(infile,"%s%s",opcode);
    while(strcmp(opcode,"END")!=0)
    {
        fscanf(infile,"%s",operand);
        if(strcmp(label,"-")!=0)
            fprintf(outfile,"%s\t%d\n",locctr);
        for(i=0;i<optablen;i++){
            if(strcmp(opcode,optab[i])==0){
                locctr=locctr+3;
                break;
            }
        }
        if(strcmp(opcode,"WORD")==0)
            locctr=locctr+3;
        else if(strcmp(opcode,"RESW")==0)
                locctr=locctr+(3*(atoi(operand)));
            else if(strcmp(opcode,"BYTE")==0)
            {
                if(operand[0]=='X')
                    locctr=locctr+1;
                else
                    locctr=locctr+(strlen(operand)-2);
            }
            else if(strcmp(opcode,"RESB")==0)
                locctr=locctr+atoi(operand);
            fscanf(infile,opcode);
    }
    printf("Length of assambled program=%d\n",locctr-start);
    fclose(infile);
    fclose(outfile);
}

这是我的代码,如何避免注释行?

charlesluo326 回答:如何使用fscanf()避免出现特定行?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3158294.html

大家都在问