I'm making an SIC 2 pass assembler in C++. And I'm not very good at files in C++. The following code segment is supposed to read LABEL OPCODE OPERAND from the input text file . All it has to do is to identify each of those based on the tab which separates them.The problem here is that it fails to read the operand and also , it is read the first line again in a disordered manner. Please help

#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main()
{
    char temp[10],tlabel[10],topcode[6],toperand[12];
    ifstream fin("inputfile.txt",ios::in);
    int a=0,b=0,i=0;
    while(fin)
    {
        i=0;
        a=0;
        b=0;

        fin.getline(temp,26);
        while((temp[i]!='\0')&&(temp[i]!='\t'))
        {
            tlabel[i]=temp[i];
            i++;
        }
        tlabel[i]='\0';
        i++;
        a=i;
        while((temp[i]!='\0')&&(temp[i]!='\t'))
        {
            topcode[i-a]=temp[i];
            i++;
        }
        topcode[i-a]='\0';
        i++;
        b=i;
        while((temp[i]!='\0'))
        {
            toperand[i-b]=temp[i];
            i++;
        }
        toperand[i-b]='\0';

    cout<<tlabel<<"\t"<<topcode<<"\t";
    cout<<"\n";
    }


       return 0;

 }

Also it is making a weird beebing noise every-time it executes. Is it reading some alert tone or something?

Use std::strings

int main(){
 string label,topcode,toperand;
 ifstream fileIn("input.txt");
 while( fileIn >> label >> topcode >> toperand){
   cout << "Label = " << label << endl;
   cout << "TopCode = "  << topcode << endl;
   cout << "Toperand = " << toperand << endl;
 }
}

Cant use std::strings .The code should be compatible with turbo C++ v3 .

Yes, but you are already using <iostream> (without the '.h' extension) and using namespace std; , neither of which would work with TC++. Or are you developing with a different compiler first and back-porting it to Turbo C++?

Yes, I have TC++ at college lab. Doing with code::blocks in my laptop and gotta back port to to Turbo C++

This article has been dead for over six months. Start a new discussion instead.