Help with a book example: binary files

This is a discussion on Help with a book example: binary files within the C++ Programming forums, part of the General Programming Boards category; Hi, I've been going through the book "C++ Without Fear" by Brian Overland and I've come across a problem with ...

  1. #1
    Registered User wtaplin's Avatar
    Join Date
    Dec 2009
    Posts
    13

    Help with a book example: binary files

    Hi, I've been going through the book "C++ Without Fear" by Brian Overland and I've come across a problem with one of the examples. It's about opening a binary file, writing data to it, and closing it. My problem is, no matter what I enter for the filename, the program can't open the file. Any help would be much appreciated. Here's the example straight out of the book:

    Code:
    // writebin.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #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();
    	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);
    }

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    If you just type in 'bindata.dat' file, the program is going to look for that file in the directory the program is residing in. If it's elsewhere, you'll need the exact and complete pathway (ie. c:\\windows\\desktop\\bindata.dat)

  3. #3
    Registered User wtaplin's Avatar
    Join Date
    Dec 2009
    Posts
    13
    The previous examples in the book dealt with files in text format, as opposed to binary format, and used the same method for opening a file. It also created the file to be opened so there was no need to create it beforehand for the program to open it. For example, if I entered stupidfile at the prompt, it created a file named stupidfile in the directory of the program. Or if I specified a path, C:\windows\desktop\stupidfile, it created 'stupidfile on the desktop. This is the relevant code block from the previous examples which ran without a hitch, it's very similar to my original binary format one. The binary format version won't accept anything for a filename and I'm not sure how to create a new .dat file beforehand to see if it can be opened.

    Code:
             cout << "Enter the filename and press ENTER: ";
    	cin.getline(filename, 80);
    
    
    	ofstream fout(filename);
    	
    	while(1) {
    	cout << "Enter the line of text to be saved and press ENTER\n";
    	cout << "or press ENTER to exit: ";
    	cin.getline(usertext, 900);
    	if(strlen(usertext) == 0){
    		break;
    	}
    	if(!fout) {
    		cout << "File" << filename;
    		cout << " could not be opened";
    		return -1;
    	}
    Last edited by wtaplin; 02-25-2010 at 07:12 PM.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Add ios::app to the list of flags you're passing to the constructor.

    EDIT: Note this you will need to rewind to read the file after opening.
    Last edited by rags_to_riches; 02-25-2010 at 07:39 PM.

  5. #5
    Registered User wtaplin's Avatar
    Join Date
    Dec 2009
    Posts
    13
    Quote Originally Posted by rags_to_riches View Post
    Add ios::app to the list of flags you're passing to the constructor.

    EDIT: Note this you will need to rewind to read the file after opening.
    Ahh it worked! Thank you, thank you : ) I'm not sure why the author left that out of the code, or more importantly why it works but I'm sure I'll get there eventually. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Copying Binary Files
    By mikeman118 in forum C++ Programming
    Replies: 9
    Last Post: 08-11-2007, 10:55 PM
  3. C++ Binary Files Problem
    By terran9 in forum C++ Programming
    Replies: 13
    Last Post: 12-15-2004, 09:23 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. binary files reference needed!
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 03-12-2002, 08:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21