title: Writing my own Cat function in C
body: <p>Hi i don't know how to simulate my own Cat function in C, i know how it works when no arguments are set and i already get it, but my problem is when i tried to open a file and then print itself...</p>
  
  <p>my code until now:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;fcntl.h&gt;
  
  int main(int argc, char* argv[])
  {  
      char *a1 = (char*) malloc (sizeof(char));
      int sz, fd,cont=0, cont1=0;
      char *b1 = (char*) malloc (sizeof(char));
      //char *a2 = (char*) malloc (sizeof(char));
      char * a2;
      char *b2 = (char*) malloc (sizeof(char));
  
      // NO PARAMETERS
      while (argc == 1){      
          sz=read(0, a1, 1);
          b1[cont]=a1[0];
  
          if(b1[cont]=='\n'){
              write(1,b1,cont);
              write(1,"\n",1);
              b1=NULL;            
          }
  
          cont=cont+1;
          b1=(char*) realloc(b1, sizeof(char)*cont);
        }
  
      // 1 PARAMETER (FILE)   /*Here is the problem*/
      if (argc &gt; 1){
  
          fd=open(argv[1],O_RDONLY);
          a2=fgetc(fd);
  
          while (a2 != EOF){
              b2[cont1]=a2;
              cont1=cont1+1;
              b2=(char*) realloc (b2, sizeof(char)*cont1+1);
              a2=fgetc(fd);
          }
  
          write(1,b2,cont);
          b2=NULL;
          close(fd);  
      }
  
      return 0;
  }
  </code></pre>
  
  <p>What am i supposed to do ? </p>