Monday 1 February 2016

Basic Filing - Handling Multiple Files (C++)

Basic Filing - Handling Multiple Files (C++)

Description:    In this program, we have 2 files. Description of these files is as follows:

Basic Filing - Handling Multiple Files (C++)


fileA

It contains code in a certain language. Format of this file will be:

operation(space)variable-name(comma)(space)variable/integer

operations: only the following operations are legal
and
mov
sub
add

operands:
any integer between 0-9
variables: ax, bx, cx, dx
Operands can only be two but variable name should be first operand. Second can be variable name or integer e.g.
mov cx, dx
is a valid statement
whereas
mov  5, ax is not

fileB

It contains the code against each keyword or variable name from fileA.
Operation-name(space)code
mov A8
add B8
sub C8
and D8
ax F1
bx F2
cx F3
dx F4

The program reads each statement from fileA and look for each of its keyword/variable’s equivalent code in fileB and produces an output fileC. For example,

mov bx, 5 will be: A8F25
However any integer operand read from fileA will be placed as it in your output file. e.g.
For the statement mov ax, 5 output statement will be A8F15
But before reading from fileA you also have to check that file for syntax errors. In case of any syntax error there will be no conversion.


C++ Code

#include <iostream>
#include <fstream>
using namespace std;
bool CHECK(int&no_of_iterations)              
{
     int i = 0, j = 0;
     char A_line[11], check[4];
     ifstream Ain("fileA.txt");
     while (!Ain.eof())
     {
           Ain.getline(A_line, 11);
           for (j = 0;j<3;j++)
                check[j] = A_line[j];
           check[j] = '\0';
           if (strcmp(check, "add") && strcmp(check, "mov") && strcmp(check, "and") && strcmp(check, "sub"))  //function
                break;
           if (A_line[j] != ' ')    //space
                break;
           j++;
           if (!(A_line[j] >= 'a' && A_line[j] <= 'd') && (A_line[j + 1] == 'x'))       //ax,bx,cx,dx
                break;
           j += 2;
           if (A_line[j] != ',')             //,
                break;
           j++;
           if (A_line[j] != ' ')    //space
                break;
           j++;
           if (!((A_line[j] >= '0'&&A_line[j] <= '9') || ((A_line[j] >= 'a' && A_line[j] <= 'd') && (A_line[j + 1] == 'x'))))
                break;
           no_of_iterations++;
     }
     if (!Ain.eof())
     {
           cout << "Syntax Error. Conversion not possible" << endl;
           return false;
     }
     return true;
}
void Ain(char oper[], char var1[], char var2[], int count)
{
     ifstream Ain("fileA.txt");
     for (int a = 1;a <= count;a++)
     {
           Ain >> oper;
           Ain.ignore(1, ' ');
           Ain.get(var1, 3, ',');
           Ain.ignore(1, ' ');
           Ain >> var2;
     }
}
void Bin(char B[][8], int b_row)
{
     ifstream Bin("fileB.txt");
     for (int i = 0;i<b_row;i++)
           Bin.getline(B[i], 7);
}
void OPER(char C[], char oper[], char B_data[][8], int b_row, int &k)
{
     int i = 0, j = 0;
     for (i = 0;i<b_row;i++)
     {
           for (j = 0;oper[j] != '\0';j++)
                if (B_data[i][j] != oper[j])
                     break;
           if (oper[j] == '\0')
                for (k = 0;k<2;k++)
                     C[k] = B_data[i][j + 1 + k];
     }
}
void VAR1(char C[], char var1[], char B_data[][8], int b_row, int &k)
{
     int i = 0, j = 0, l = 0;
     for (i = 0;i<b_row;i++)
     {
           for (j = 0;var1[j] != '\0';j++)
                if (B_data[i][j] != var1[j])
                     break;
           if (var1[j] == '\0')
                for (k = 2, l = 0;k<4;k++, l++)
                     C[k] = B_data[i][j + 1 + l];
     }
}
void VAR2(char C[], char var2[], char B_data[][8], int b_row, int &k)
{
     int i = 0, j = 0, l = 0;
     if (var2[0] >= '0' && var2[0] <= '9')
     {
           C[k] = var2[0];
           k++;
     }
     else
     {
           for (i = 0;i<b_row;i++)
           {
                for (j = 0;var2[j] != '\0';j++)
                     if (B_data[i][j] != var2[j])
                           break;
                if (var2[j] == '\0')
                     for (k = 4, l = 0;k<6;k++, l++)
                           C[k] = B_data[i][j + 1 + l];
           }
     }
}
int main()
{
     const int b_row = 8, b_col = 8;
     int k = 0, count = 1, no_of_iterations = 0;
     char oper[5], var1[4], var2[4], B_data[b_row][b_col], C[7];
     if (CHECK(no_of_iterations))
     {
           Bin(B_data, b_row);
           ofstream Cout("fileC.txt");
           for (int a = 1;a <= no_of_iterations;a++)
           {
                k = 0;
                Ain(oper, var1, var2, count);
                OPER(C, oper, B_data, b_row, k);
                VAR1(C, var1, B_data, b_row, k);
                VAR2(C, var2, B_data, b_row, k);
                count++;
                C[k] = '\0';
                Cout << C << endl;
           }
           return 0;
     }
}

No comments

Post a Comment

Recent Posts