Do/While Loop for binary file reading

I believe there should be 4 packets of data inside my packets.txt file, so I created a do/while loop to read each packet. My code only steps through two packets before closing out. I think its a simply error at my ch input just before the while as a result of the "/n" character read in once I hit enter for yes or no. Does C++ allow for just entered y/n to continue looping without hitting enter?

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
  #include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype>
#include <vector>

using namespace std;

struct s_header {
	unsigned char verCode;
	unsigned char serviceType;
	unsigned short totLength;
	unsigned char reserved;
	unsigned char flags;
	unsigned short hopCount;
	unsigned char srcAddr0;
	unsigned char srcAddr1;
	unsigned char srcAddr2;
	unsigned char srcAddr3;
	unsigned char destAddr0;
	unsigned char destAddr1;
	unsigned char destAddr2;
	unsigned char destAddr3;
} header;

char * codes[] = {
	"OK",
	"Possible data corruption"
};

char * services[] = {
	"Normal",
	"Low Priority",
	"High Priority",
	"Medium Priority",
	"So-so Priority",
	"Needed Yesterday",
	"Nevermind"
};

char * flags[] = {
	"Grumpy",
	"Sneezy",
	"Bashful",
	"Doc",
	"Dopey",
	"Tipsy",
	"Surly",
	"Snow White"
};

int main(int argc, char * argv)
{
	const char* const file_name = "PACKETS.txt";
	int x;
	char ch;
	ifstream file(file_name, ios::in | ios::binary); // open for input in binary mode
	if (!file.is_open())
	{
		std::cerr << "failed to open file\n";
		return 1;
	}
	do
	{
		s_header header; //create object of s_header struct
		const auto hdr_size = sizeof(header); //get size of header file
		if (file.read(reinterpret_cast<char*>(std::addressof(header)), hdr_size) &&
			file.gcount() == hdr_size)
		{
			// print out contents of header in a readable form, not needed here...
			const auto idata_size = header.totLength - 16;
			vector<char> buffer(idata_size);
			if (file.read(buffer.data(), buffer.size()) && file.gcount() == idata_size)
			{
				// display data from header
				cout << "Version:" << setw(9) << (header.verCode >> 4) << endl;
				cout << "Code:" << setw(12) << (header.verCode & 0x0F) << " - " << codes[(header.verCode & 0x0f)] << endl;
				cout << "Total Length:" << setw(5) << header.totLength << endl;
				cout << "Service Type:" << setw(4) << (int)header.serviceType << " - " << services[header.serviceType] << endl;
				//display flags using a mask and loop
				x = header.flags;
				for (int i = 0; i < 8; i++)
				{
					cout << "    " << flags[i] << ":\t" << (x & 1) << endl;
					x = x >> 1;
				}
				cout << "Hop Count:" << setw(8) << header.hopCount << endl;
				cout << "Source Address:" << setw(4) << (int)header.srcAddr0 << "." << (int)header.srcAddr1 << "." << (int)header.srcAddr2
					<< "." << (int)header.srcAddr3 << endl;
				cout << "Dest Address:" << setw(4) << (int)header.destAddr0 << "." << (int)header.destAddr1 << "." << (int)header.destAddr2
					<< "." << (int)header.destAddr3 << endl;
				cout << "Data:" << endl;
				for (int i = 0; i < idata_size; i++)
				{
					cout << (char)buffer[i];
				}
			}
			else
			{
				cerr << "failed to read data\n";
				return 1;
			}
		}
		else
		{
			std::cerr << "failed to read header\n";
			return 1;
		}

		cout << ("Read in next packet? Y/N ");
		ch = getchar();
		cout << "\n" << endl;
	} while (ch == 'Y' || ch == 'y');

	//cout << endl;
	system("pause");
}
Topic archived. No new replies allowed.