Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to read and write to a BINARY file?

How to read and write to a BINARY file?

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dipuks
    wrote on last edited by
    #1

    Hello Can anyone tell me how to read and write to a binary file? Any sample code will be of great help. I want to write multiple structures into a binary file and then read it one by one. Thanks in advance

    M enhzflepE A 3 Replies Last reply
    0
    • D dipuks

      Hello Can anyone tell me how to read and write to a binary file? Any sample code will be of great help. I want to write multiple structures into a binary file and then read it one by one. Thanks in advance

      M Offline
      M Offline
      Member 4194593
      wrote on last edited by
      #2

      Just CreateFile with binary access, write it, seek to the front, and read it back record by record. If you want random access, then the records must be the same size. If you want variable length records, just precede each binary record with a DWORD with the record size when you write it, then when you read it back, read the DWORD to get the record size, then read the data block, - this also implies that the file must be sequentially accessed both for writing and reading. Dave.

      1 Reply Last reply
      0
      • D dipuks

        Hello Can anyone tell me how to read and write to a binary file? Any sample code will be of great help. I want to write multiple structures into a binary file and then read it one by one. Thanks in advance

        enhzflepE Offline
        enhzflepE Offline
        enhzflep
        wrote on last edited by
        #3

        #include <stdio.h>

        typedef struct _myStruct
        {
        int x;
        int y;
        int z;
        } myStruct;

        // use this main() to create the file
        int main()
        {
        FILE *fp;
        char *fileName = "output.dat";
        char *mode = "wb";
        myStruct theArray[10];
        int i;
        for (i=0; i<10; i++)
        {
        theArray[i].x = i;
        theArray[i].y = 10-i;
        theArray[i].z = i * (10-i);
        }
        fp = fopen(fileName, mode);
        fwrite(theArray, sizeof(struct myStruct), 10, fp);
        fclose(fp);
        }

        // use this main() to read the file
        /*
        int main()
        {
        FILE *fp;
        char *fileName = "output.dat";
        char *mode = "rb";
        myStruct theArray[10]={};
        int i;
        fp = fopen(fileName, mode);
        fread(theArray, sizeof(myStruct), 10, fp);
        fclose(fp);
        for (i=0; i<10; i++)
        {
        printf("Item %d---------\n", i+1);
        printf("<%d, %d, %d>\n\n", theArray[i].x, theArray[i].y, theArray[i].z);
        }
        system("pause");
        return 0;
        }
        */

        D 1 Reply Last reply
        0
        • enhzflepE enhzflep

          #include <stdio.h>

          typedef struct _myStruct
          {
          int x;
          int y;
          int z;
          } myStruct;

          // use this main() to create the file
          int main()
          {
          FILE *fp;
          char *fileName = "output.dat";
          char *mode = "wb";
          myStruct theArray[10];
          int i;
          for (i=0; i<10; i++)
          {
          theArray[i].x = i;
          theArray[i].y = 10-i;
          theArray[i].z = i * (10-i);
          }
          fp = fopen(fileName, mode);
          fwrite(theArray, sizeof(struct myStruct), 10, fp);
          fclose(fp);
          }

          // use this main() to read the file
          /*
          int main()
          {
          FILE *fp;
          char *fileName = "output.dat";
          char *mode = "rb";
          myStruct theArray[10]={};
          int i;
          fp = fopen(fileName, mode);
          fread(theArray, sizeof(myStruct), 10, fp);
          fclose(fp);
          for (i=0; i<10; i++)
          {
          printf("Item %d---------\n", i+1);
          printf("<%d, %d, %d>\n\n", theArray[i].x, theArray[i].y, theArray[i].z);
          }
          system("pause");
          return 0;
          }
          */

          D Offline
          D Offline
          dipuks
          wrote on last edited by
          #4

          Thanks for the sample code. I am working on C++ using WIN32 API's in Visual Studio 2003. In the function here for binart read/write, i am using the functions, _write and _ read. Does that make any difference in writing/reading? compared to using fwrite and fread?

          enhzflepE D 2 Replies Last reply
          0
          • D dipuks

            Thanks for the sample code. I am working on C++ using WIN32 API's in Visual Studio 2003. In the function here for binart read/write, i am using the functions, _write and _ read. Does that make any difference in writing/reading? compared to using fwrite and fread?

            enhzflepE Offline
            enhzflepE Offline
            enhzflep
            wrote on last edited by
            #5

            Please, allow me to refer you to this short discussion: fread vs _read[^]

            1 Reply Last reply
            0
            • D dipuks

              Thanks for the sample code. I am working on C++ using WIN32 API's in Visual Studio 2003. In the function here for binart read/write, i am using the functions, _write and _ read. Does that make any difference in writing/reading? compared to using fwrite and fread?

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              dipuks wrote:

              i am using the functions, _write and _ read.

              See here and here.

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              1 Reply Last reply
              0
              • D dipuks

                Hello Can anyone tell me how to read and write to a binary file? Any sample code will be of great help. I want to write multiple structures into a binary file and then read it one by one. Thanks in advance

                A Offline
                A Offline
                Adam Roderick J
                wrote on last edited by
                #7

                For opening binary file use CreateFile [^] with OPEN_EXISTING flag. For reading the file use ReadFile [^] For writing the file use WriteFile [^]

                Величие не Бога может быть недооценена.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups