0

Hello, I'm writing a program where I take a prewritten text file and a. convert it into binary, b. write text to that binary file, c. display an output report and d. sort by employee name. I can do a,c and d, but I am having a hrad time with b. If some one could just steer me in teh right direction on where I'm going wrong in the dataEntryToBinary class I would really appreciate it. Also I am posting the text file that accompanies the program.

#include "stdafx.h"
#include "conio.h"
#include "string.h"
#include <iostream>
#include <fstream>
//#include <cctype> // for toupper
//#include <ctype.h>

using namespace std;

struct production_report
{
    char sEname[16];
    char sProduct[6];
    int iUnits;
    float fCost;
};

class convertToBinary
{
public:
    void convertToBinary_Start(void);

private:
    production_report data;
    fstream myFile_01;
    fstream myFile_02;

    void mainLine(void);
    void initialization(void);
    void process(void);
    void eoj(void);

};

class dataEntryToBinary
{
public:
    // Member Functions
    void dataEntryToBinary_Start(void);

private:
    // Member Data
    char *myReturn;
    production_report data;
    char ans[10];
    fstream myFile;

    // Member Functions
    void mainLine(void);
    void initialization(void);
    void process(void);
    void eoj(void);
};

void printDetail(void);


int main(void)
{
    char mySelect;

    convertToBinary cTB;
    dataEntryToBinary dETB;

    cout << "        Make Your Men Selection\n";
    cout << "  1. Conver the Text data file to Binary." << endl;
    cout << "  2. Enter data into Binary file." << endl;
    cout << "  3. Display production report." << endl;
    cout << "  4. Sort report by employee name." << endl;
    cout << "  5. End the program." << endl;
    cout << endl << "  Make your selection: ";

    mySelect = _getch();

    cout << mySelect << endl;

    while (mySelect != '5')
    {

        if (mySelect == '1')
        {
            cTB.convertToBinary_Start();
        }
        else if (mySelect == '2')
        {
            dETB.dataEntryToBinary_Start();
        }
        else if (mySelect == '3')
        {
            printDetail();
        }
        else if (mySelect == '4')
        {

        }

        cout << "        Make Your Men Selection\n";
        cout << "  1. Conver the Text data file to Binary." << endl;
        cout << "  2. Enter data into Binary file." << endl;
        cout << "  3. Display production report." << endl;
        cout << "  4. Sort report by employee name." << endl;
        cout << "  5. End the program." << endl;
        cout << endl << "  Make your selection: ";

        mySelect = _getch();

        cout << mySelect << endl;

    }

    cout << endl;
    cout << "The program has ended." << endl;

    return 0;
}

void convertToBinary::convertToBinary_Start(void)
{
    mainLine();

}

void convertToBinary::mainLine(void)
{
    initialization();

    while (myFile_01)
    {
        process();
    }

    eoj();

}

void convertToBinary::initialization(void)
{
    myFile_01.open("C:\\employeedata.txt", ios::in);

    // Make connection with new binary file

    myFile_02.open("C:\\empData.bin", ios::out | ios:: binary);

    if (!myFile_02)
    {
        cout << "Error opening file. Program aborting.\n";
        return;
    }
}

void convertToBinary::process(void)
{
    // Read data from file.

    // First read in the string.
    myFile_01.getline(data.sEname, 16, '\n');

    // Then read numbers.
    myFile_01 >> data.sProduct >> data.iUnits >> data.fCost;

    if (myFile_01)
    {
        myFile_02.write(reinterpret_cast<char *>(&data), sizeof(data));

        // Last clear the buffer of a return.
        myFile_01.get();
    }
}

void convertToBinary::eoj(void)
{
    myFile_01.close();
    myFile_02.close();
}

void dataEntryToBinary::dataEntryToBinary_Start(void)
{

    mainLine();

}

void dataEntryToBinary::mainLine(void)
{
    initialization();
    process();
    eoj();
}

void dataEntryToBinary::initialization(void)
{

    myFile.open("c:\\employeedata.bin", ios::out | ios:: binary | ios::app);
    if (!myFile)
    {
        cout << "Error opening file. Program aborting.\n";
        return;
    }


}

void dataEntryToBinary::process(void)
{
    cout << "Please enter the name of the employee" << endl;
    cin >> data.sEname;

    cout << "Product Number: ";
    cin >> data.sProduct;

    cout << "Units Produced: ";
    cin >> data.iUnits;

    cout << "Unit Cost: ";
    cin >> data.fCost;

    // Show data on screen (what was entered)
    cout << data.sEname << endl;
    cout << data.sProduct << " " << data.iUnits  << " " << data.fCost << endl;

    // Write data to a binary file.
    myFile.write(reinterpret_cast<char *>(&data), sizeof(data));


}

void dataEntryToBinary::eoj(void)
{
    myFile.close();

}




// Text File

Kay Archer 
P9511 42 2.98
Alan Baum 
A1234 24 5.50
Marie Fitch 
C4510 36 7.94
Lee HildeBrand 
R0934 18 6.75
David Mullins 
E3371 36 3.79
Chad Nelson 
L8912 20 4.33
Bill Quinn 
S0951 48 5.65
Nicole Renner
H9733 24 4.25
Erica Tate
Z0182 27 8.10
Terry West
A3235 30 2.95
Ann Zimmerman
N4578 98 3.25
Terry Perkins
P3789 19 6.30

Edited by alhaji.m.kanu: Edit

2
Contributors
1
Reply
30
Views
3 Years
Discussion Span
Last Post by ravenous
1

To get a better understanding, the task

b. write text to that binary file

Does that mean that you have to read in the text file and then write the text into a new file, which is a binary file? Or, does it mean that you have a pre-existing binary file that you have to add text to?

If it's the second one, then you have to read in the binary file into a new set of objects, make the changes that you want to make (add new text in this case) and then re-write the updated objects to the binary file.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.