title: reading input from text file into array of structures in c
body: <p>My structure definition is,</p>
  
  <pre><code>typedef struct {
      int taxid;
      int geneid;
      char goid[20];
      char evidence[4];
      char qualifier[20];
      char goterm[50];
      char pubmed;
      char category[20];    
  } gene2go;
  </code></pre>
  
  <p>I have tab-seperated text file called `"gene2go.txt".</p>
  
  <p>Each line of this file contains <code>taxID</code>, <code>geneID</code>, <code>goID</code>, <code>evidence</code>, <code>qualifier</code>, <code>goterm</code>, <code>pubmed</code> and <code>category</code> information.</p>
  
  <p>Each line of the file will be kept in a structure.</p>
  
  <p>When the program is run, it will first read the content of the input file into an array of type gene2go, I used a function called <code>readInfo</code>.</p>
  
  <p>The program will also take the following input arguments from the command line, </p>
  
  <p>input type ( 1 for <code>taxid</code>, 2 for <code>geneid</code>, 3 for <code>goid</code> ) and search term</p>
  
  <p>There is a function called <code>listData</code> to write the list of lines in the file “gene2go.txt” that match the input type and search term, to the file “output.txt”.</p>
  
  <p>Here is a part of my text file <code>"gene2go.txt"</code>,</p>
  
  <pre><code>3702    814629  GO:0003676  IEA -   nucleic acid binding    -   Function
  3702    814629  GO:0005575  ND  -   cellular_component  -   Component
  3702    814629  GO:0005634  ISM -   nucleus -   Component
  3702    814629  GO:0008150  ND  -   biological_process  -   Process
  3702    814629  GO:0008270  IEA -   zinc ion binding    -   Function
  3702    814630  GO:0005634  ISM -   nucleus -   Component
  3702    814636  GO:0008150  ND  -   biological_process  -   Process
  3702    814637  GO:0003674  ND  -   molecular_function  -   Function
  6239    177883  GO:0008150  ND  -   biological_process  -   Process
  6239    177884  GO:0005575  ND  -   cellular_component  -   Component
  6239    177884  GO:0008150  ND  -   biological_process  -   Process
  6239    177886  GO:0004364  IDA -   glutathione transferase activity    12757851    Function
  6239    177886  GO:0005575  ND  -   cellular_component  -   Component
  7955    555450  GO:0005634  IEA -   nucleus -   Component
  7955    555450  GO:0006355  IEA -   regulation of transcription, DNA-dependent  -   Process
  </code></pre>
  
  <p>I have written the code below named ceng301.c and compiled it using the command line</p>
  
  <p><code>gcc ceng301.c -o ceng301</code></p>
  
  <p>, but when I write</p>
  
  <p><code>ceng301 1 3702</code></p>
  
  <p>in the command line, I get nothing, but a blinking underscore :( instead of</p>
  
  <pre><code>3702    814629  GO:0003676  IEA -   nucleic acid binding    -   Function
  3702    814629  GO:0005575  ND  -   cellular_component  -   Component
  3702    814629  GO:0005634  ISM -   nucleus -   Component
  3702    814629  GO:0008150  ND  -   biological_process  -   Process
  3702    814629  GO:0008270  IEA -   zinc ion binding    -   Function
  3702    814630  GO:0005634  ISM -   nucleus -   Component
  3702    814636  GO:0008150  ND  -   biological_process  -   Process
  3702    814637  GO:0003674  ND  -   molecular_function  -   Function
  </code></pre>
  
  <p>saved in <code>output.txt</code></p>
  
  <p>Here is the code,</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  /* structure definition */
  typedef struct {
      int taxid;
      int geneid;
      char goid[20];
      char evidence[4];
      char qualifier[20];
      char goterm[50];
      char *pubmed;
      char category[20];
  } gene2go;
  
  /* function prototypes */
  int readInfo( gene2go input[] );
  void listData( char *inType, char *searchItem, gene2go input[], int i );
  
  int main( int argc, char *argv[] )
  {
     gene2go input[200];
     int i;
  
     i = readInfo( input );
     listData( argv[1], argv[2], input, i );
  
     return 0;
  }
  
  /* read the input file*/
  int readInfo( gene2go input[] ) {
     FILE *fin;
     char *inputName = "gene2go.txt";
     int i = 0;
  
     fin = fopen( inputName, "r" );
  
     if( fin == NULL ) {
        printf( "File cannot be opened\n" );
     } /* end if */
     else {
        while( !feof( fin ) ) {
          fscanf( fin, "%[^\t]", &amp;input[i].taxid,
                                 &amp;input[i].geneid,
                                 &amp;input[i].goid,
                                 &amp;input[i].evidence,
                                 &amp;input[i].qualifier,
                                 &amp;input[i].goterm,
                                 &amp;input[i].pubmed,
                                 &amp;input[i].category );
          i++;
       } /* end while */
  
       fclose( fin );
    } /* end else */
  
    return i;
  } /* end function readInfo */
  
  void listData( char *inType, char* searchItem, gene2go input[], int i ) {
     FILE *fout;
     char *outputName = "output.txt";
     int j;
     int inputType = atoi( inType );
  
     fout = fopen( outputName, "w" );
  
     switch( inputType ) {
        case 1:
            for( j = 0; j &lt; i; j++ ) {
                 if( input[j].taxid == atoi( searchItem ) ) {
                     fprintf( fout, "%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s\n", input[i].taxid,
                                                                        input[i].geneid,
                                                                        input[i].goid,
                                                                        input[i].evidence,
                                                                        input[i].qualifier,
                                                                        input[i].goterm,
                                                                        input[i].pubmed,
                                                                        input[i].category );
                 } /* end if */
            } /* end for */
            break;
       case 2:
            for( j = 0; j &lt; i; j++ ) {
                if( input[j].geneid == atoi( searchItem ) ) {
                    fprintf( fout, "%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s\n", input[i].taxid,
                                                                       input[i].geneid,
                                                                       input[i].goid,
                                                                       input[i].evidence,
                                                                       input[i].qualifier,
                                                                       input[i].goterm,
                                                                       input[i].pubmed,
                                                                       input[i].category );
                } /* end if */
            } /* end for */
            break;
       case 3:
            for( j = 0; j &lt; i; j++ ) {
                if( input[j].goid == searchItem ) {
                    fprintf( fout, "%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s\n", input[i].taxid,
                                                                       input[i].geneid,
                                                                       input[i].goid,
                                                                       input[i].evidence,
                                                                       input[i].qualifier,
                                                                       input[i].goterm,
                                                                       input[i].pubmed,
                                                                       input[i].category );
                } /* end if */
            } /* end for */
            break;
   } /* end switch */
  
   fclose( fout );
  } /* end function listData */
  </code></pre>
  
  <p>What should I do?</p>