0

I have a school project. I have to write a basic virtual machine in C that is able to host a CoreWar game. I am supposed to read from a file written in binary, but I am not allowed to use fopen, fread or fseek.

I have to use read, write and lseek.

I really dont understand how I am supposed to do this, everything I found on internet says I have to use fopen with the "rb" mode.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Henry
  • 149
  • 2
  • 8
  • 6
    Use `open` system call with `O_BINARY` option (if your compiler says that the option is undefined, remove it). Did you google `read`, `write` and `lseek`? Hint: they are also system calls, so adding it to the query will help filtering the results. – MikeCAT Mar 10 '16 at 14:36
  • I did google read write lseek and open, but I have not come across anything like the O_BINARY option you mentioned. I guess I am not that good at googling. Anyway, thanks for the very fast answer! – Henry Mar 10 '16 at 14:40
  • try `man read` even on Google. Then look for "See also" section. – Eugene Sh. Mar 10 '16 at 14:42
  • Which platform are you on ? – Jabberwocky Mar 10 '16 at 14:54
  • I am on Ubuntu Linux – Henry Mar 10 '16 at 15:19
  • O_BINARY doesnt seem to exist... I am getting "error: ‘O_BINARY’ undeclared (first use in this function)" – Henry Mar 10 '16 at 15:22
  • Henry, did you get the information you need? Are you going to accept an answer? – George Mar 10 '16 at 18:39
  • 1
    You could add something like `#ifndef O_BINARY`, `#define O_BINARY 0`, `#endif` to define it conditionally. It isn't needed for Linux anyway, because there is no difference between text and binary modes in Linux. – Ian Abbott Mar 10 '16 at 19:17
  • Read [Advanced Linux Programming](http://www.makelinux.net/alp/) – Basile Starynkevitch Mar 10 '16 at 20:17

2 Answers2

4

Here's a complete example of reading the file using the low-level functions you are required to use.

Replace the comment /* Process the data */ with your own code that does something useful with the data you read.

   int rfd;   /* File descriptor. */
   char buffer[BUFFER_SIZE];   /* Buffer to put file content into */
   int bufferChars; /* number of characters returned by the read function */

   /* Open the file */
   if ((rfd = open(argv[1], O_RDONLY, 0)) < 0)
      perror("Open failed.");

   /* Read and process the file */
   while (1)
   {
      /* Normal case --- some number of bytes read. */
      if ((bufferChars = read(rfd, buffer, BUFFER_SIZE)) > 0)
      {
           /* Process the data */
      }
      else if (bufferChars == 0)   /* EOF reached. */
         break;
      else   /* bufferChars < 0 --- read failure. */
         perror("Read failed.");
   }

   close(rfd);
George
  • 2,436
  • 4
  • 15
  • 30
  • Your answer made me understand what I didnt get. I dont actually have to open with a particular option/flag to read a binary file. I just have to read it like any other file. The comments on the question above now make a lot more sense. Thanks for the good help. – Henry Mar 10 '16 at 21:03
3

you might consider using mmap() for reading the file data. Check this answer here: When should I use mmap for file access?

Community
  • 1
  • 1
user2021201
  • 370
  • 3
  • 10