So I've been reading through book's, browsing forums, and watching videos to learn C++. So far, it's been pretty smooth to learn. However, I've gotten to the part in my book (C++ without fear) where it goes over Binary files. Now, I understand what they are and how they work, they just seem really complicated to impliment into code. So my question is: Will reading and writing to binary files be a useful thing to remember all about it? If so, I'll crack down and really learn this. Or is there a better method to doing this and I just haven't learned it yet, and I can just kind of grasp the concept and move on? It just seems to me a lot to grasp and remember, and this book is just breezing over it in detail like it's nothing to important. Any pointers on this?
You'll probably want to read or write binary files at some point.
I can't quite follow you though, what exactly is there to learn or remember about binary files?
There seems to be a lot to me. Reading and writing to a text file was pretty simple and strait forward. With a binary file though there is a lot more to it, and a lot more to remember. Like I need to know bite length and all that jazz. To give you an idea of what I'm trying to learn, here is the code the book is using to teach me.
I read and write binary files far more often than text files, FWIW
I don't really see what's so complicated about them. Instead of a string of characters (like a text file), you have a string by bytes. They're virtually identical.
#include <iostream>
#include <fstream>
usingnamespace std;
int get_int(int default_value);
int main() {
char filename[MAX_PATH + 1];
int n = 0;
char name[20];
int age = 0;
int recsize = sizeof(name) + sizeof(int);
cout << "Enter file name: ";
cin.getline(filename, MAX_PATH);
fstream fbin(filename, ios::binary | ios::out);
if (!fbin) {
cout << "Could not open " << filename << endl;
system("PAUSE");
return -1;
}
cout << "Enter file record number: ";
n = get_int(0);
cout << "Enter name: ";
cin.getline(name, sizeof(name) - 1);
cout << "Enter age: ";
age = get_int(0);
fbin.seekp(n * recsize);
fbin.write(name, sizeof(name) - 1);
fbin.write((char*)(&age), sizeof(int));
fbin.close();
system("PAUSE");
return 0;
}
#define COL_WIDTH 80
int get_int(int default_value)
{
char s[COL_WIDTH+1];
cin.getline(s, COL_WIDTH);
if (strlen(s) == 0)
return default_value;
return atoi(s);
}
And btw, there is no need to remember everything about C++ so long as you know where to find the answers and understand the general concept. If you have that book then just use it as a reference when you need it.
I don't really see what's so complicated about them. Instead of a string of characters (like a text file), you have a string by bytes. They're virtually identical.
The idea and how they work is not complicated, I agree. I understand how they work. But the coding itself is more complicated to me. I was just wondering how often (speculating) as a programmer I might use this, so I know if I need to remember all, or just know how it works and look back if I need to know the details.