Read data from binary file C++

in #hive4 years ago

Hello everyone welcome back to my channel to day i am providing you What is binary file in C++. In this series i use Visual studio for coding.

Download Visual studio

  • Choose Empty project.
    image.png
  • Click Create to start.
    Screenshot%20(524).png

  • double click on source files, add and add new item.
    Screenshot%20(525).png

  • Click on C++ file, fill your solution name then click on add. Okay let start.
    Screenshot%20(526).png

Okay let me show you some benefits of binary file in C++:

  • Access faster.
  • light.
  • Security your data.
  • Easier to access than text file.
  • Read one for all, write one for all.

To start we have to include fstream to manipulation on file.
image.png

To manipulation with file we have two concept:

  • ifstrem(use for read data from file to program).
  • ofstream(write data from program into file).
  • So now we start with ifstream. back to desktop import a text file and than rename it's tail from .txt to .dat. After that right mouse, choose properties then copy it's location and past into your program.
    Screenshot%20(528).png

  • All of the following is syntax so you can copy from mine. Remember to use binary mode and change forward slash to back slash because in C++ '' is special character and remember use this sentence readfromfile.close(); before return 0; if you don't have that your program will be scratch.
    Screenshot%20(529).png

  • Let's let user input what they want

    1. declare a struct Data in it we have an interger n use to save data when we read from file.
      image.png

    2. use syntax read to read all data from file to program. Finish.
      image.png

    3. if your program well the exited code will 0.
      image.png

That all about read data from binary file. Thank for reading. Remember practice, challenge and not affraid of difficulties.