I am making this post not because I am not understanding a specific coding error this time, but rather because the examples of code in my book don't work.
The book I am trying to teach myself C++ from is "C++ Without Fear" by Brian Overland.
I've reached a point in my book where I've started doing some work with creating external files (particularly text files, and engaging in the process of writing to them). The problem is, I have begun to find that both the codes I copy for myself from the book into a compiler, as well as the codes from the "Answer Sheet" sort of folder that came with a disc contained within the book, do not work.
The big problem with this of course is that, although the author does a great job of explaining the code and how it works in the "How It Works" part of every section, after any given coding exercise, I find that I learn best by analyzing a code as I run the .exe, examining each process bit by bit by comparing what appears on the console output, to how it is handled within the code itself.
Let me give you an example.
#include <iostream>
#include <fstream>
using namespace std;
int get_int(int default_value);
char name[20];
int main() {
char filename[81];
int n;
int age;
int recsize = sizeof(name) + sizeof(int);
cout << "Enter file name: ";
cin.getline(filename, 80);
// Open file for binary read and write.
fstream fbin(filename, ios::binary | ios::in | ios::out);
if (!fbin) {
cout << "Could not open file " << filename;
return -1;
}
// Get record number to write to.
cout << "Enter file record number: ";
n = get_int(0);
// Get data from end user.
cout << "Enter name: ";
cin.getline(name, 19);
cout << "Enter age: ";
age = get_int(0);
// Write data to the file.
fbin.seekp(n * recsize);
fbin.write(name, 20);
fbin.write(reinterpret_cast<char*>(&age), sizeof(int));
fbin.close();
system("pause");
return 0;
}
// Get integer function
// Get an integer from keyboard; return default
// value if user enters 0-length string.
//
int get_int(int default_value) {
char s[81];
cin.getline(s, 80);
if (strlen(s) == 0)
return default_value;
return atoi(s);
}
This code is from the "answer sheet" folder. Meaning of course that this is the code written by Brian (or someone else, I don't know) but should theoretically be accurate.
Both this code and my own have the same problem.
The program should supposedly create a binary output file with data on the user's name and age.
This is my first time doing things with binary files, and I'm already finding them somewhat complex. Admittedly, having the exercises not work for me is greatly impairing my ability to understand the idea behind certain processes, how to use certain syntax, and more importantly, how these bits of syntax, and things (in this example) such as binary file writing could be at all useful in more productive working programs.
Does anyone have any suggestions on how I could get through this problem?
If anyone is curious, I do have on my computer two other C++ for beginners books by the titles of "C++ - How to program" and "C++ primer fifth edition".
Question
Ryuurei
Hello again everyone.
I am making this post not because I am not understanding a specific coding error this time, but rather because the examples of code in my book don't work.
The book I am trying to teach myself C++ from is "C++ Without Fear" by Brian Overland.
I've reached a point in my book where I've started doing some work with creating external files (particularly text files, and engaging in the process of writing to them). The problem is, I have begun to find that both the codes I copy for myself from the book into a compiler, as well as the codes from the "Answer Sheet" sort of folder that came with a disc contained within the book, do not work.
The big problem with this of course is that, although the author does a great job of explaining the code and how it works in the "How It Works" part of every section, after any given coding exercise, I find that I learn best by analyzing a code as I run the .exe, examining each process bit by bit by comparing what appears on the console output, to how it is handled within the code itself.
Let me give you an example.
#include <iostream> #include <fstream> using namespace std; int get_int(int default_value); char name[20]; int main() { char filename[81]; int n; int age; int recsize = sizeof(name) + sizeof(int); cout << "Enter file name: "; cin.getline(filename, 80); // Open file for binary read and write. fstream fbin(filename, ios::binary | ios::in | ios::out); if (!fbin) { cout << "Could not open file " << filename; return -1; } // Get record number to write to. cout << "Enter file record number: "; n = get_int(0); // Get data from end user. cout << "Enter name: "; cin.getline(name, 19); cout << "Enter age: "; age = get_int(0); // Write data to the file. fbin.seekp(n * recsize); fbin.write(name, 20); fbin.write(reinterpret_cast<char*>(&age), sizeof(int)); fbin.close(); system("pause"); return 0; } // Get integer function // Get an integer from keyboard; return default // value if user enters 0-length string. // int get_int(int default_value) { char s[81]; cin.getline(s, 80); if (strlen(s) == 0) return default_value; return atoi(s); }This code is from the "answer sheet" folder. Meaning of course that this is the code written by Brian (or someone else, I don't know) but should theoretically be accurate.
Both this code and my own have the same problem.
The program should supposedly create a binary output file with data on the user's name and age.
This is my first time doing things with binary files, and I'm already finding them somewhat complex. Admittedly, having the exercises not work for me is greatly impairing my ability to understand the idea behind certain processes, how to use certain syntax, and more importantly, how these bits of syntax, and things (in this example) such as binary file writing could be at all useful in more productive working programs.
Does anyone have any suggestions on how I could get through this problem?
If anyone is curious, I do have on my computer two other C++ for beginners books by the titles of "C++ - How to program" and "C++ primer fifth edition".
Thanks.
Link to comment
Share on other sites
41 answers to this question
Recommended Posts