Do I really need to know this well?

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?
Last edited on
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
using namespace 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);
}


There seems to be a lot to learn and remember. =\
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.
Do you clean your code up while using it?

I could not imagine working in that mess.

This looks much better.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <fstream>
using namespace 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.
Last edited on
I usually clean up my code, but that was strait copied form the book so it copied funky. And thanks the advice.
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.
But the coding itself is more complicated to me.


I personally find processing text to be more difficult than reading a binary file. But to each his own I guess.

I was just wondering how often (speculating) as a programmer I might use this


Depends entirely on what you will be programming. You might use it all the time or you might never use it.
I've practised through it a bit now, and you're right it's not that hard really. Just got to use it. :D Thanks for the insight.
Topic archived. No new replies allowed.