text file to binary file conversion

Hello. I'm writing a program using structures and classes to turn a text file into a binary and also other things. I've written the first part of the code down and I think that it is correct. however every time I run it, the program creates a massive vlc file in my C drive. Along with the code I'm posting the accompanying text file. If someone could tell me what I'm doing wrong i would really appreciate it

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
  #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[15];
	char sProduct[5];
	int iUnits[3];
	float fCost[3];
};

class convertToBinary
{
public:
	void convertToBinary_Start(void);

private:
	bool bEndFlag;
	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 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')
		{
			
		}
		else if (mySelect == '3')
		{
			
		}
		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;
}

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

}

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

	while (bEndFlag == false)
	{
		process();
	}

	eoj();

}

void convertToBinary::initialization(void)
{
	bEndFlag = false;
	myFile_01.open("c:\\employeedata.txt", ios::in);

	// Do a priming read first

		// Read data from file.
		// First read in the string.
		myFile_01.getline(data.sEname, '\n');
		// Then read numbers, after checking for eof.

		if (myFile_01.eof() == true)
		{
			bEndFlag = true;
		}
		else
		{
			// Then read numbers.
			myFile_01 >> data.sProduct >> data.iUnits[3] >> data.fCost[3];
			// Last clear the buffer of a return.
			myFile_01.get();
		}

	// 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)
{
		myFile_02.write(reinterpret_cast<char *>(&data), sizeof(data));

		// Read data from file.
		// First read in the string.
		myFile_01.getline(data.sEname, 15, '\n');
		myFile_01.getline(data.sProduct, 5, '\n');
		// Then read numbers.

		if (myFile_01.eof() == true)
		{
			bEndFlag = true;
		}
		else
		{
			// Then read numbers.
			myFile_01 >> data.iUnits[3] >> data.fCost[3];
			// Last clear the buffer of a return.
			myFile_01.get();
		}
}

void convertToBinary::eoj(void)
{
	myFile_01.close();
	myFile_02.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
25
//Text file data that is being converted
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
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();
}

Last edited on
Thank you very much, I was able to properly get the program to compile and I can finish the program now. Again thank you very much.
Topic archived. No new replies allowed.