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. Read and write binary

Read and write binary

Scheduled Pinned Locked Moved C / C++ / MFC
comdata-structurestoolshelpquestion
7 Posts 4 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.
  • T Offline
    T Offline
    The ANZAC
    wrote on last edited by
    #1

    Can anyone help me, or at least point me to some good sites that will help with reading and writing binary files on the fly without using an array?

    Please check out my articles: The ANZAC's articles

    D 1 Reply Last reply
    0
    • T The ANZAC

      Can anyone help me, or at least point me to some good sites that will help with reading and writing binary files on the fly without using an array?

      Please check out my articles: The ANZAC's articles

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

      Binary files and arrays have nothing to do with the other.

      "Love people and use things, not love things and use people." - Unknown

      "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

      T 1 Reply Last reply
      0
      • D David Crow

        Binary files and arrays have nothing to do with the other.

        "Love people and use things, not love things and use people." - Unknown

        "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

        T Offline
        T Offline
        The ANZAC
        wrote on last edited by
        #3

        Ok then, well that only answered part of my question. How do i read and write a binary file, what are some keywords i should be looking up?

        Please check out my articles: The ANZAC's articles

        S D enhzflepE 3 Replies Last reply
        0
        • T The ANZAC

          Ok then, well that only answered part of my question. How do i read and write a binary file, what are some keywords i should be looking up?

          Please check out my articles: The ANZAC's articles

          S Offline
          S Offline
          sashoalm
          wrote on last edited by
          #4

          you can use fopen, fread, fwrite, and finally fclose. google for those functions.

          There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

          1 Reply Last reply
          0
          • T The ANZAC

            Ok then, well that only answered part of my question. How do i read and write a binary file, what are some keywords i should be looking up?

            Please check out my articles: The ANZAC's articles

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

            That all depends on what you are using to open/create the file. Some possibilities include:

            // C
            fopen()
            CreateFile()

            // MFC
            CFile

            // C++
            ofstream

            Each of these has their own way of opening/creating a binary file.

            "Love people and use things, not love things and use people." - Unknown

            "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

            1 Reply Last reply
            0
            • T The ANZAC

              Ok then, well that only answered part of my question. How do i read and write a binary file, what are some keywords i should be looking up?

              Please check out my articles: The ANZAC's articles

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

              With fopen, fread and fwrite. Here's a rough n nasty example:

              #include <stdio.h>
              #include <stdlib.h>

              int main()
              {
              FILE *fp;
              long i;
              long fLen;
              long tmpValue;

              // try to open an existing file
              fp = fopen("test.dat", "r+b");
              // if failed, then create a file
              if (fp == NULL)
              {
                  printf("File doesn't exist\\n");
                  printf("creating...\\n");
                  fp = fopen("test.dat", "w+b");
              }
              // if file opened/created ok continue
              if (fp)
                  printf("File opened successfully.\\n");
              // otherwise, nick-off
              else
                  return -1;
              
              // write some binary data to the file
              for (i=0; i<100; i++)
                  fwrite(&i, sizeof(i), 1, fp);
              
              // get length of file
              fseek(fp, 0, SEEK\_END);
              fLen = ftell(fp);
              
              // return file pointer to beginning of file
              fseek(fp, 0, SEEK\_SET);
              
              // loop through, reading 1 long at a time from the file and printing it's value
              for (i=0; i<fLen/sizeof(long); i++)
              {
                  fread(&tmpValue, sizeof(long), 1, fp);
                  printf("%4d", tmpValue);
              }
              
              fclose(fp);
              return 0;
              

              }

              T 1 Reply Last reply
              0
              • enhzflepE enhzflep

                With fopen, fread and fwrite. Here's a rough n nasty example:

                #include <stdio.h>
                #include <stdlib.h>

                int main()
                {
                FILE *fp;
                long i;
                long fLen;
                long tmpValue;

                // try to open an existing file
                fp = fopen("test.dat", "r+b");
                // if failed, then create a file
                if (fp == NULL)
                {
                    printf("File doesn't exist\\n");
                    printf("creating...\\n");
                    fp = fopen("test.dat", "w+b");
                }
                // if file opened/created ok continue
                if (fp)
                    printf("File opened successfully.\\n");
                // otherwise, nick-off
                else
                    return -1;
                
                // write some binary data to the file
                for (i=0; i<100; i++)
                    fwrite(&i, sizeof(i), 1, fp);
                
                // get length of file
                fseek(fp, 0, SEEK\_END);
                fLen = ftell(fp);
                
                // return file pointer to beginning of file
                fseek(fp, 0, SEEK\_SET);
                
                // loop through, reading 1 long at a time from the file and printing it's value
                for (i=0; i<fLen/sizeof(long); i++)
                {
                    fread(&tmpValue, sizeof(long), 1, fp);
                    printf("%4d", tmpValue);
                }
                
                fclose(fp);
                return 0;
                

                }

                T Offline
                T Offline
                The ANZAC
                wrote on last edited by
                #7

                Thanks, this looks quite helpful, i will get to work on this :)

                Please check out my articles: The ANZAC's articles

                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