I'm using turbo C++. For my SIC assembler I need to read user the input assembly language program from a text file.Each line of the program contains a maximum of 3 words , LABEL OPCODE and OPERAND. LABEL or OPERAND or both can be absent by OPCODE is a must.
The following code of mine reads line by line from the input file using getline()
Now I want to split that line into those 3 strings. Also any number of spaces or tabs can be present in the line.
How do I use sscanf for this purpose?
int readinput(){
c=0;
char temp[40],tlabel[10],topcode[6],toperand[12];
ifstream fin("inputfile2.txt",ios::in);
int a=0,b=0,i=0;
while(fin.getline(temp,30))
{
i=0;
a=0;
b=0;
tlabel[0] ='\0';
topcode[0] ='\0';
toperand[0]='\0';
// here is where the splitting of temp to label[if present] opcode and operand[if present] should take place
cout<<tlabel<<"\t"<<topcode<<"\t"<<toperand<<"\n";
strcpy(sc[c].label,tlabel);
strcpy(sc[c].opcode,topcode);
strcpy(sc[c].operand,toperand);
c++;
}
fin.close();
return 1;
}