title: Using systems calls (open, read, write) to display file content
body: <p>I'm trying to implement a command called displaycontent that takes a text file name as argument and display its contents. I am to use <code>open()</code>, <code>read()</code>, <code>write()</code>, and <code>close()</code> system calls in Linux to do this. It should act somewhat like the UNIX <code>cat</code> command for displaying file content.</p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;string.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;unistd.h&gt;
  
  int main(int argc, char *argv[])
  {
      int fd;
      char content[fd];   
  
      errno = 0;
      fd = open(argv[1], O_RDONLY);
  
  
      if(fd &lt; 0)
      {
          printf("File could not be opened.\n");
          perror("open");
          return 1;
      }
      else 
      {
  
          read(fd, content, sizeof(content)-1);
          write(1, content, sizeof(content)-1);
      }
  
  return 0;
  }
  </code></pre>
  
  <p>I have a file named hello2.txt and in it, there's the text: <code>hellooooooooooooooo</code></p>
  
  <p>When I do <code>./displaycontent hello2.txt</code>, I get:</p>
  
  <pre><code>user@user-VirtualBox:~/Desktop/Csc332/csc332lab$ ./displaycontent hello2.txt
  hellooooooooooooooo
  ����&gt;k���[`�s�b��user@user-VirtualBox:~/Desktop/Csc332/csc332lab$ 
  </code></pre>
  
  <p>There are strange symbols and things following the content of the file. I am not sure what is wrong, any help would be appreciated. Thank you.</p>