Writing to a binary file

This is a discussion on Writing to a binary file within the C++ Programming forums, part of the General Programming Boards category; I need to wright an array 'membership' to a binary file members.dat but am having no luck at all :O( ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    1

    Exclamation Writing to a binary file

    I need to wright an array 'membership' to a binary file
    members.dat but am having no luck at all :O(

    The function I am having trouble with is save_new_member()

    Any comments/suggestions would be greatly appreciated!

    Whole program is below as requested! It compiles in emacs, not sure about anything else.



    #include <iostream.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    #include <string.h>

    enum Boolean {FALSE, TRUE};
    const int STRING_LENGTH = 20;
    const int MAX_MEMBER = 50;

    // Structure for holding member information
    struct member_type
    {
    // Add fields here for member records
    char name[20]; // must include the null character
    int age;
    long number;
    char gender[7]; // must include the null character
    Boolean fees_paid;
    };


    void process_requests();
    void obtain_member (member_type &this_member);
    void display_member(member_type this_member);
    void do_update();
    void do_add();
    void do_view();
    void do_view_unpaid(); // added this function in P3
    void do_count();
    void save_new_list();
    void read_old_list();
    Boolean find_member(char members_name[STRING_LENGTH], int &member);
    void do_find();



    // The number of current members
    int number_of_members = 0;

    //Global variable to hold a array of members P3.
    member_type membership[MAX_MEMBER];



    void main()
    {
    read_old_list();
    process_requests();
    save_new_list();
    }


    void process_requests()
    {
    char command;
    do
    {
    cout << endl;
    cout << "a(dd), s(earch), v(view), p(view unpaid), u(update), f(inished): ";
    cin >> command;

    switch (command)
    {
    case 'a':
    do_add();
    break;
    case 'v':
    do_view();
    break;
    case 'p':
    do_view_unpaid();
    break;
    case 'u':
    do_update();
    break;
    case 's':
    do_find();
    break;
    }
    } while (command != 'f');
    }



    //reads in data for a club member from the keyboard and stores
    //it in the record

    void obtain_member (member_type &this_member)
    /*
    Function which prompts the user for and reads all
    information for a new member.

    PRE : This_member holds no new member information
    POST : this_member holds the new member information
    */
    {
    char paid; // Temporary Variable
    Boolean loop = TRUE; // While loop equals true, it will continue to loop

    cout << "Name: ";
    cin >> this_member.name;
    cout << "Age: ";
    cin >> this_member.age;
    cout << "Phone Number: ";
    cin >> this_member.number;
    cout << "Gender: ";
    cin >> this_member.gender;

    do
    {
    cout << "Has this member paid their fees? y/n: ";
    cin >> paid;

    if (paid == 'Y' || paid == 'y')
    {
    this_member.fees_paid = TRUE;
    loop = FALSE;
    }
    else if (paid == 'N' || paid == 'n')
    {
    this_member.fees_paid = FALSE;
    loop = FALSE;
    }
    else
    cout << "ERROR\n";
    }
    while(loop == TRUE);
    }


    void display_member(member_type this_member)
    /*
    This function displays the information on this_member

    PRE : this_member holds the appropriate info
    POST : information on this_members has been displayed
    */
    {
    // Check to see that appropriate data in structure is displaying itself correctly
    cout << "Name: " << this_member.name << "\t";
    cout << "Age: " << this_member.age << "\t";
    cout << "Phone: " << this_member.number << "\t";
    cout << "Gender: " << this_member.gender << "\t";
    cout << "Fees Paid: ";

    if (this_member.fees_paid == TRUE)
    cout << "1 \n";
    else if (this_member.fees_paid == FALSE)
    cout << "0 \n";


    }




    void do_add()
    /*
    Function to add a new member to the current list of members.

    PRE : number_of_members and the membership array hold the appropriate info
    POST : a new member has been added to the array of members
    */
    {
    // This code puts the membership into the array at 0, and increments the number
    // of members (refer to number_of_members being a counter)
    obtain_member(membership[number_of_members]);
    number_of_members++;
    }


    void do_view()
    /*
    This function displays the information on all members

    PRE : number_of_members and membership hold the appropriate info
    POST : information on all members has been displayed
    */
    {

    for(int i=0; i<number_of_members; i++)
    display_member(membership[i]);


    }



    void do_count()
    /*
    This function calculates and displays how many members have not
    paid their subscription

    PRE : number_of_members and membership hold the appropriate info
    POST : count of unpaid members has been displayed
    */
    {

    }


    void save_new_list()
    /*
    This function updates the membership file by writing all member
    info to the file.

    PRE : membership holds number_of_members amount of member info
    POST : the membership file has been updated and now holds the same
    information as the membership array.
    */
    {


    int i=0;
    ofstream outfile; // object name of data file is here

    outfile.open("members.dat", ios:: binary || ios::out); // open the file members.dat
    // check to see if the file is opened correctly
    if (!outfile)
    {
    cout << "\nThe file was not successfully opened"
    << "\nPlease check that the file currently exists."
    << endl;
    exit(1);
    }

    //THIS IS THE BIT IM HAVING TROUBLE WITH!!!!
    //IS THIS CORRECT???????
    outfile.write(&membership[i], sizeof(membership[i]));
    i++;

    outfile.close();


    }


    void read_old_list()
    /*
    This function reads in all of the member information from the
    member file and stores it in the membership array. number_of_members
    is assigned the total number of members read in. If the file cannot
    be opened, the program should continue with number_of_members set to 0.

    PRE :
    POST : number_of_members holds the number of members stored
    in the member file and the membersip array holds all member info.
    */
    {


    char ch;
    int i=0;
    ifstream infile; // object name of data file is here

    infile.open("members.dat", ios:: binary || ios::in); // open the file members.dat
    // check to see if the file is opened correctly
    if (infile.fail())
    {
    cout << "\nThe infile was not successfully opened"
    << "\nPlease check that the file currently exists."
    << endl;
    exit(1);
    }

    while( (ch = infile.peek()) !=EOF ) //check next character
    {
    infile.read(&membership[i], sizeof(membership[i]));
    i++;
    number_of_members++;
    }

    cout<< "Total members in array: " << number_of_members << endl;


    }


    Boolean find_member(char members_name[STRING_LENGTH], int &member)
    /*
    This function attempts to find a member with name members_name
    in the list of known members. If the member is found then the
    variable, member, is assigned the appropriate membership index
    and the function returns true, otherwise the function returns
    false.

    PRE : number_of_members and membership hold the appropriate info
    POST : if the member was found then member holds the members
    membership index.
    */
    {
    return (FALSE);
    }


    void do_find()
    /*
    This function asks the user for the name of a member to look for
    and tries to find that member in the list of known members. If
    the member is found the index is displayed otherwise the user is
    told that the member was not found.

    PRE : number_of_members and membership hold the appropriate info
    POST :
    */
    {
    char aname[STRING_LENGTH];
    int index;

    cout << endl;
    cout << "Who are you looking for? ";
    cin >> aname;

    if (find_member(aname,index))
    cout << "Found " << aname << " member index is " << index << endl;
    else
    cout << "Sorry - could not find " << aname << endl;
    }


    void do_view_unpaid()
    {
    /*
    This function displays the information on all members
    who have not paid their fee's

    PRE : number_of_members and membership hold the appropriate info
    POST : information on all unpaid members has been displayed
    */

    int i;

    for(i=0; i<number_of_members; i++)
    // add an if statement here to only display the unpaid member
    if (membership[i].fees_paid == FALSE)


    // displays the data of that particular member
    display_member(membership[i]);

    }
    Last edited by nz_cutechick; 08-15-2002 at 03:14 AM.

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    Re: Writing to a binary file

    Originally posted by nz_cutechick
    //IS THIS CORRECT?????
    outfile.write(&membership[ i ], sizeof(membership[ i ]));
    i++;
    Typecast membership as a char *, ie:
    Code:
    outfile.write((char *)&membership[ i ],sizeof(membership[ i ]));
    Also, are you supposed to be saving all the data in the membership array or are you saving indice 0 then later appending indice 1, etc.? If you're appending to the data then add ios::app in your outfile.open() line.

    Oh, and for future referrence use CODE TAGS. It makes reading your code A LOT easier. If you don't know how:
    put [ c o d e ] before the code in the post and [ / c o d e ] after it (take out the spaces between the brackets and the letters for it to work).

  3. #3
    Registered User Unreg1stered's Avatar
    Join Date
    Jul 2002
    Posts
    25

    Re: Writing to a binary file

    Originally posted by nz_cutechick
    ...the whole program is attached as a microsoft word document.
    Any comments/suggestions would be greatly appreciated!
    Can you post the whole program again? It might be easier to find the problems.

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Re: Re: Writing to a binary file

    Originally posted by Unreg1stered
    Can you post the whole program again? It might be easier to find the problems.
    Do not copy the code into your reply if it's too big. Just attach the file(s).

    And please nz_cutechick, use code tags when you're posting code. Just type &#91code] and then paste your code. At the end of your code type &#91;/code]

  5. #5
    Unreg1stered
    Guest

    Lightbulb

    Change

    PHP Code:
     //THIS IS THE BIT IM HAVING TROUBLE WITH!!!!
    //IS THIS CORRECT???????
    outfile.write(&membership[i], sizeof(membership[i]));
    i++; 
    to:

    PHP Code:
    // there should be on more troubles
    outfile.write(reinterpret_cast<char*>(&membership), number_of_members*sizeof(member_type));
      
    // i++; 
    The program is interesting, but the search function is not working correctly. I couldn't find one fuction, can you paste the do_update() function?

    Anyway, you should be able to save the members info to the file now.

  6. #6
    Registered User Unreg1stered's Avatar
    Join Date
    Jul 2002
    Posts
    25
    darn... I forgot to logon...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Replies: 2
    Last Post: 05-09-2008, 08:27 AM
  3. Replies: 1
    Last Post: 12-10-2005, 11:25 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM

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