title: C program of accessing device file don't work
body: <p>I have seen <a href="https://stackoverflow.com/questions/3520459/linux-hard-disk-direct-access-without-any-fs-from-c-program&quot;">device file can be accessed directly in Linux</a> and I want to have a try. I have a free disk partition without any file system. My test code is below.
  I expect to get output <code>read data: 199</code> when I run the program at the second time. But actually, I get output <code>read data: 0</code> twice. No errors emerge during the program. I have no idea where is wrong.<br>
  Thanks for your time.  </p>
  
  <p>Test Code:  </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  
  int main(){
      int num = 0;
      int fd = open("/dev/sda6", O_RDWR);
  
      if(fd == -1){
          fprintf(stderr, "open device failed, errno : %s(%d) \n", 
              strerror(errno), errno);
          return 1;
      }
  
      ssize_t ret = read(fd, &amp;num, sizeof(int));
      if(ret != sizeof(int)){
          fprintf(stderr, "read fails, errno : %s(%d) \n", 
              strerror(errno), errno);
          return 1;
      }
      printf("read data: %d\n", num);
  
      num = 199;
      ret = write(fd, &amp;num, sizeof(int));
      if(ret != sizeof(int)){
          fprintf(stderr, "write fails, errno : %s(%d) \n", 
              strerror(errno), errno);
          return 1;
      }
      close(fd);
  
      return 0;
  }
  </code></pre>