data entry into a binary file
May 1, 2013 at 4:47pm UTC
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 hard time with b. If some one could just steer me in the 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
#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();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
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
Last edited on May 1, 2013 at 5:07pm UTC
May 1, 2013 at 6:25pm UTC
myFile.write(reinterpret_cast <char *>(&data), sizeof (data));
I doubt if the above line will work.
Edit: what is the exact problem you are having ?
Last edited on May 1, 2013 at 6:32pm UTC
Topic archived. No new replies allowed.