A book example I can't get to work...

This is a discussion on A book example I can't get to work... within the C++ Programming forums, part of the General Programming Boards category; This example writes binary data to a file. It prompts for a record #, name, age. Everything works except I'm ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    46

    A book example I can't get to work...

    This example writes binary data to a file. It prompts for a record #, name, age. Everything works except I'm getting garbage for the age.

    Here's the code:
    Code:
    #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 filename: ";
    cin.getline(filename, MAX_PATH) ;
    
    // Open file for binary write.
    
    fstream  fbin(filename, ios::binary | ios::out);
    
    if (!fbin) {
    cout << "Could not open " << filename << endl ;
    system(" PAUSE" ) ;
    return - 1;
    }
    //  Get record number to wri te to.
    
    cout << "Enter file record number: ";
    n = get_int(0) ;
    
    // Get data from end user.
    
    cout << "Enter name: " ;
    cin.getline(name, sizeof(name) - 1) ;
    cout << "Enter age: " ;
    age = get_int(0) ;
    
    // Write data to the file.
    
    fbin.seekp(n * recsize) ;
    fbin.write(name, si zeof(name) - 1) ;
    fbin.write((char*)(&age) ,sizeof(int)) ;
    fbin.close() ;
    system("PAUSE" ) ;
    return 0;
    }
    #define COL_WIDTH 80  // 80 is typical column width
    
    // Get integer function
    // Get an integer from keyboard; return default
    //  value if user enters 0-l ength string.
    //
    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) ;
    }
    I thought the problem might be with this line(10):

    int recsize = sizeof(name) + sizeof(int);

    where sizeof(int) should be sizeof(age)

    and this line of code as well:

    fbin.write((char*)(&age) ,sizeof(int)) ;

    where sizeof(int) should be sizeof(age)

    I made the changes and tried it and the same garbabe for the age output.
    Any ideas why this isn't working?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    How are you reading the data back in to see that it's not working? (Note that since you are using write, the contents of the file are not human-inspectable.)

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    How are you looking at the file to determine whether it worked or not?

    If you're using a text editor, you will see "garbage" because you're writing binary files.

    You need a hex editor (see Hex editor - Wikipedia, the free encyclopedia) to correctly view a binary file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    46
    Sorry, it also prompts to enter a file name. I've been entering c:\test.txt.

    Then just check the file. The record spacing seems to work and the name I enter works, the age is just the only thing.

    maybe I shouldn't give the filename an extension. Let me try that.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't think the extension matters. What matters is what you're using to read it in. If you are using "you" to read it in, then that's a problem. You need to write a program that reads it in, or learn how to read hex as Salem mentions above. (For instance, if you type in an age of 65, do you see A in your file? That would be a good thing if so.)

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    46
    There was a second example that showed how to read out the file from my first example. When I tried that it worked. I guess I didn't understand the difference between binary data and text data.

    I also downloaded a free hex editor (Hex Editor Neo) to view the files and all seems to make sense now...well sort of.

    Thanks for the help guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Better tutorials(Book would work too)
    By Mordacai in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2007, 02:16 PM
  2. My C++ book
    By Carlos in forum C++ Programming
    Replies: 4
    Last Post: 05-25-2004, 03:32 AM
  3. Newbie trying to work w/ KR book!?!
    By Slimcracker in forum C Programming
    Replies: 3
    Last Post: 05-01-2003, 02:04 PM
  4. program from book wont work
    By cemock in forum C Programming
    Replies: 2
    Last Post: 03-06-2003, 08:58 AM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM

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