title: Reading a file line by line and compare in c
body: <p>I am not very good at explaining problems, but here's a try. I have to create a c program that reads a file written in assembly language (for the LC-2 ISA) and produces a hexadecimal representation of the program.</p>
  
  <p>So, my assembler should be able to provide translations for the LC-2 based instructions,
  for eg: LEA, AND, ADD, LDR, BR (and they have their own binary representation).</p>
  
  <p>So basically, all you need to do is read a file line by line and convert the binary to hexadecimal. So far, I have been able to only read the first two characters of the first line. Here's what I have attempted (and I apologize if I have not been clear with the question - again) -</p>
  
  <p>(again my question is, how do I read the file "File.asm" line by line and then compare if they are 'LEA' or 'ADD' or 'AND' and then based on the comparison convert the binary to hexadecimal?</p>
  
  <pre><code>#
  
  include&lt;stdio.h&gt;
      #include&lt;stdlib.h&gt;
  
  
  
      #define LEA "1110"
      #define AND "0101"
      #define ADD "0001"
      #define LDR "0110"
      #define TRAP "1111"
      #define BR "0000"
      #define Base "0011"
      #define ST "0011"
      #define DR "10"
      #define SR "010"
      #define OFF9 "01"
      #define SR1 "0101"
      #define SR2 "0111"
      #define index6 "0101"
      #define nato "0000"
      #define zato "0001"
      #define pato "0000"
      #define TVECT "00100101"
      #define OFF5 "0101"
  
  
  
      void asm_reader();
      void convert(char binaryNumber[1000]);
  
      int main()
      {
         asm_reader();
  
  
         return 0;
      }
  
  
      void asm_reader()
      { 
          FILE *myFile;
          myFile = fopen("File.asm", "r");
          char line[80];
          char array[2];
          int i;
               if (myFile == NULL)
               {
                  printf("Error Reading File\n");
                  exit (0);
               }
        while(fgets(array, 80, myFile) != NULL)
        {
      // Checks first 2 instruction characters
          for (i = 0; i &lt; 2; i++)
             {
                fscanf(myFile, "%c,", &amp;array[i] );
             }
  
  
          printf("Number is: %c", array[0]);
  
      // LEA condition________________________________________
        if ( array[0] == 'L'&amp;&amp; array[1]=='E')
      {
         //get whole line of instruction convert to binary and then to hex
         char x[50]=LEA;
         char y[50]=DR;
         char z[50]=OFF9;
  
         convert(x);
         convert(y);
         convert(z);
  
      }
  
      // AND* condition________________________________________
        if ( array[0] == 'A'&amp;&amp; array[1]=='N'){
  
  
         char x[10]=AND;
         char y[10]=SR1;
         char z[10]=SR2;
  
         convert(x);
         convert(y);
         convert(z);
       }
  
      // LDR condition________________________________________
        if ( array[0] == 'L'&amp;&amp; array[1]=='D'){
      //get whole line of instruction convert to binary and then to hex
  
      char x[10]=LDR;
      char y[10]=DR;
      char t[10]=Base;
      char z[10]=index6;
  
      convert(x);
      convert(y);
      convert(t);
      convert(z);
  
          }
  
      //____________________________________________________
  
      // BR condition________________________________________
        if ( array[0] == 'B'&amp;&amp; array[1]=='R'){
      //get whole line of instruction convert to binary and then to hex
  
      char x[10]=BR;
      char n1[10]=nato;
      char z1[10]=zato;
      char p1[10]=pato;
      char y[10]=OFF9;
  
  
      convert(x);
      convert(n1);
      convert(z1);
      convert(p1);
      convert(y);
          }
  
  
      //____________________________________________________
  
      // ADD* condition________________________________________
        if ( array[0] == 'A'&amp;&amp; array[1]=='D'){
      //get whole line of instruction convert to binary and then to hex
  
      char x[10]=ADD;
      char n1[10]=DR;
      char z1[10]=SR1;
      char y[10]=OFF5;
  
  
      convert(x);
      convert(n1);
      convert(z1);
      convert(y);
  
          }
  
  
  
      //____________________________________________________
  
  
      // TRAP condition________________________________________
        if ( array[0] == 'T'&amp;&amp; array[1]=='R'){
      //get whole line of instruction convert to binary and then to hex
      char x[10]=TRAP;
      char n1[10]=DR;
      char z1[10]=SR1;
      char y[10]=OFF5;
  
  
      convert(x);
      convert(n1);
      convert(z1);
      convert(y);
          }
  
  
  
      //____________________________________________________
  
      // ST condition________________________________________
        if ( array[0] == 'S'&amp;&amp; array[1]=='T'){
      //get whole line of instruction convert to binary and then to hex
      char x[10]=ST;
      char n1[10]=SR;
      char z1[10]=OFF9;
  
  
      convert(x);
      convert(n1);
      convert(z1);
      }
  
      //____________________________________________________
  
          fclose(myFile);
  
  
       }
      }
  
      //Converter Method. From binary to Hex
      void convert(char binaryNumber[1000]){
  
  
       int temp;
      long int i=0,j=0;
  
      char hexaDecimal[1000]; 
  
       while(binaryNumber[i]){
            binaryNumber[i] = binaryNumber[i] -48;
            ++i;
         }
  
         --i;
         while(i-2&gt;=0){
             temp =  binaryNumber[i-3] *8 + binaryNumber[i-2] *4 +  binaryNumber[i-1] *2 + binaryNumber[i] ;
             if(temp &gt; 9)
                  hexaDecimal[j++] = temp + 55;
             else
                  hexaDecimal[j++] = temp + 48;
             i=i-4;
         }
  
      if(i ==1)
            hexaDecimal[j] = binaryNumber[i-1] *2 + binaryNumber[i] + 48 ;
         else if(i==0)
            hexaDecimal[j] =  binaryNumber[i] + 48 ;
          else
            --j;
  
         printf("Equivalent hexadecimal value: ");
      FILE *opFile;
      opFile = fopen("mama.asm", "a");
         while(j&gt;=0){
  
      fprintf(opFile,"%s",&amp;hexaDecimal[j]);
  
            printf("%c",hexaDecimal[j--]);
  
         }
      fclose(opFile);
      }
  </code></pre>