fscanf有时会停止工作

时间:2018-04-21 20:17:42

标签: c++ file assembly scanf

对于起始几行的工作正常然后停止工作。

while(!feof(fp)){



    char cc=fgetc(fp);fputc(cc,o);putchar(cc);

    if(cc==EOF)break;
    if(cc=='\n'){
    char aa=fgetc(fp);
    fseek(fp,-1,SEEK_CUR);

    if(aa==' '){

    fscanf(fp,"%s",&mne);
    fprintf(o,"\t%s\t",mne);
    printf("\t%s\t",mne);

    if(!strcmp(mne,"RSUB")){strcat(objectCode[count].n,get_opcode(mne));
                            printf("%s\n",objectCode[count].n);count++;
                            continue;}
    fscanf(fp,"%s",&var);
    fprintf(o,"%s",var);
    printf("%s\t",var);

    char buff[10];
    strcat(objectCode[count].n,get_opcode(mne));
    sprintf(buff,"%0X",get_add(var));
    strcat(objectCode[count].n,buff);
    fprintf(o,"%s\n",objectCode[count].n);
    printf("%s\n",objectCode[count].n);
    count++;
    continue;
    }
    cout<<"\nno tabs\n";


            **fscanf(fp,"%s%s%s",&a,&mne,&var);**
            fprintf(o,"%s\t%s\t%s",a,mne,var);
            printf("%s\t%s\t%s\t",a,mne,var);

            if(!strcmp(mne,"BYTE"))
            {
                    char buff[4];
                    for(int i=2;i<(strlen(var)-1);i++)
                    {
                            sprintf(buff,"%0X",(int)var[i]);
                            strcat(objectCode[count].n,buff);

                    }
                    fprintf(o,"%s\n",objectCode[count].n);
                    printf("%s\n",objectCode[count].n);
                    count++;
                    continue;


            }
            else if(!strcmp(mne,"WORD"))
            {
                    char buff[10];
                    sprintf(buff,"%d",get_add(var));
                    strcat(objectCode[count].n,buff);
                    fprintf(o,"%s\n",objectCode[count].n);
                    printf("%s\n",objectCode[count].n);
                    count++; continue;
            }
            else if(!strcmp(mne,"RESW"))
            {
                    strcat(objectCode[count].n,"------");
                    fprintf(o,"%s\n",objectCode[count].n);
                    printf("%s\n",objectCode[count].n);
            }

            else if(!strcmp(mne,"RESB"))
            {
                    strcat(objectCode[count].n,"------");
                    fprintf(o,"\t%s\n",objectCode[count].n);
                    printf("%s\n",objectCode[count].n);
            }

            else
            {
                    char buff[10];
                    strcat(objectCode[count].n,get_opcode(mne));
                    sprintf(buff,"%0X",get_add(var));
                    strcat(objectCode[count].n,buff);
                    fprintf(o,"%s\n",objectCode[count].n);
                    printf("%s\n",objectCode[count].n);
            }
    count++;
    }

}

我正在尝试从文件中读取以下内容并生成操作码。

    COPY    START   1000
FIRST   STL     RETADR
CLOOP   JSUB    RDREC
        LDA     LENGTH
        COMP    ZERO
        JEQ     ENDFIL
        JSUB    WRREC
        J       CLOOP
ENDFIL  LDA     EOF
        STA     BUFFER
        LDA     THREE
        STA     LENGTH
        JSUB    WRREC
        LDL     RETADR
        RSUB
EOF     BYTE    C'EOF'
THREE   WORD    3
ZERO    WORD    0
RETADR  RESW    1
LENGTH  RESW    1
BUFFER  RESB    4096

RDREC   LDX     ZERO
        LDA     ZERO
RLOOP   TD      INPUT
        JEQ     RLOOP
        RD      INPUT
        COMP    ZERO
        JEQ     EXIT
        STCH    BUFFER,X
        TIX     MAXLEN
        JLT     RLOOP
EXIT    STX     LENGTH
        RSUB
INPUT   BYTE    X'F1'
MAXLEN  WORD    4096

WRREC   LDX     ZERO
WLOOP   TD      OUTPUT
        JEQ     WLOOP
        LDCH    BUFFER,X
        WD      OUTPUT
        TIX     LENGTH
        JLT     WLOOP
        RSUB
OUTPUT  BYTE    X'05'
        END     FIRST
  

直到这一行才能正常工作:EOF BYTE C'EOF'   我试过指标,它确实在这一行之后进入循环,但是从fscanf行不起作用:                   的的fscanf(fp的, “%S%S%S”,&安培;一(a)安培; MNE,&安培; VAR);

然后陷入僵局。 fscanf停止工作。请帮忙。可能是什么问题?

1 个答案:

答案 0 :(得分:1)

刺的一个问题是可选标签。

大多数行的格式为:

<optional-label> <instruction> <operands>  

由于每条指令都在一个单独的行上,因此逐行读取文件并解析该行似乎是最好的问题(而不是像C ++这样的更自由的表单语言)。

所以我们从基础程序开始:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

void Process_Text_Line(const std::string& text_line)
{
   std::cout << text_line << "\n";
}

int main()
{
  std::ifstream program_file("my_program.asm");
  std::string text_line;
  while (std::getline(program_file, text_line);
  {
     // For now, echo the instruction
     Process_Text(text_line);
  }
  return EXIT_SUCCESS;
}

让这个工作。

下一步,向Process_Text_Line方法添加更多内容,例如识别标签,说明和操作数:

#include <vector>

void Process_Text_Line(const std::string& text_line)
{
   std::vector<std::string> instructions =
     {
        "STL", "JSUB",
        //...
     };

   // Extract the first "word".
   std::istringstream instruction_stream(text_line);
   std::string word;
   instruction_stream >> word;

   // If the word is not an instruction, consider it a label.
   std::string label;
   std::string opcode;
   if (std::find(instructions.begin(), instructions.end(), word) != instructons.end())
   {
      label = word;
      instruction_stream >> opcode;
   }
   else
   {
     opcode = word;
   }
   // Extract the operand
   std::string operand;
   instruction_stream >> operand;
   // Now print them:  
   std::cout << "Label: " << label;
   std::cout << ",  opcode: " << opcode;
   std::cout << ",  operand: " << operand << "\n";
   }

一旦提取了标签,操作码和操作数,就可以直接处理它们。可能存在例外情况,其中操作码具有多个操作数或零操作数。

使用C ++流和字符串的一个很好的问题是可以简单地处理可变长度记录。例如,如果操作码没有操作数,则提取将生成错误,但处理继续。不用担心是否向scanf提供一个,两个或三个格式说明符或参数。

相关问题