There are several separate issues, which cause the program to malfunction, and then enter an infinite loop.
1. the structure
production_report
does not match the data in the input text file.
The first part is ok, the longest name is "Lee HildeBrand" - 14 characters. Allowing for a null terminator,
char sEname[15];
is just long enough. But it might be wise to allow a bit more space for longer names, say 25 or 30 characters.
The next bit, "P9511" requires 6 characters including the null terminator. char sProduct[5]; is too small.
Next, there is a single integer followed by a single floating-point value. I don't see why the struct allocates an array of three of each.
When it comes to reading the data, this command reads the first 10 characters
myFile_01.getline(data.sEname, '\n');
Why 10? Well, the newline character '\n' has an ASCII code of 10.
The second parameter in the getline function is the size
http://www.cplusplus.com/reference/istream/istream/getline/
As a result, part of the name will often remain unread. The next input operation on the file will access this, and everything gets out of sync. As a result, the
fail flag is set for myFile_01 and any subsequent input operation will fail.
In addition, because the program tests for the eof() flag, which remains clear, the loop will never terminate.
Also this line
myFile_01 >> data.iUnits[3] >> data.fCost[3];
is accessing the arrays out of bounds. There are three elements, indexes 0 to 2 are valid. element [3] is the 4th element, which is outside the array.
So - here's a modified (and slightly trimmed, for clarity) version of the code:
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
|
#include "conio.h"
#include <cstring>
#include <iostream>
#include <fstream>
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);
};
int main()
{
convertToBinary cTB;
cTB.convertToBinary_Start();
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();
}
|