Binary files in c++
This video tutorial explains what are binary files, how binary file differs from a normal text file, how to open a binary file, how to read and write to the binary file using get and put methods in detail with example.
source code for this tutorial
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
char input[100];
strcpy(input,"learning lad rocks");
fstream file("anil.bin",ios::binary | ios:: in | ios::out | ios::trunc);
if(!file.is_open()){
cout << "error while opening the file";
}else{
int length = strlen(input);
for(int counter = 0; counter <= length; counter++){
file.put(input[counter]);
}
file.seekg(0);
char ch;
while(file.good()){
file.get(ch);
cout << ch;
}
}
return 0;
}