Help with different binary file read options

Any tips on how to modify the second set of code (after comment) to do what the the first set of code does? I would like to use an ifstream object instead of fread.


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
 int main(int argc, char * argv) {
	int bytesRead, idataSize, x;
	char buffer[800], user_answer;
	FILE * fp = 0;
	fp = fopen("PACKETS.txt", "r");

	if (fp == 0) 
	{
		cerr << "Error: Could not open file" << endl;
		exit(1);
	}
		// read in header
		bytesRead = fread(&header, 1, 16, fp);

		// read and display data portion of packet
		idataSize = header.totLength - 16; // calculate data size
		fread(buffer, 1, idataSize, fp); // read data into buffer

// I want to use something like this.. but this doesn't open the file correctly...
int main(int argc, char * argv[]) {
	char buffer[1024];
	char ch, code;
	int x;

	ifstream myfile("PACKETS.txt", ios::in | ios::binary);
	if (!myfile) {
		cout << "Cannot open file.\n";
		return 1;	
	}

	myfile.read((char *)& fileheader, sizeof s_header);
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <iomanip>

int main()
{
    const char* const file_name = __FILE__ ; // this file
    std::ifstream file( file_name, std::ios::binary ) ; // open for input in binary mode

    char buffer[1024] {} ;
    file.read( buffer, sizeof(buffer) ) ;
    const auto nread = file.gcount() ;

    for( std::streamsize i = 0 ; i < nread ; ++i )
    {
        std::cout << std::hex << std::setw(2) << std::setfill('0') << int(buffer[i]) << ' ' ;
        if( i%16 == 15 ) std::cout << '\n' ;
    }
    
    std::cout << "\n-----------------------\n" << std::dec << nread << " bytes were read\n" ;
}

http://coliru.stacked-crooked.com/a/b5b48186ee787d3e
JLBorges, I would like to actually incorporate that into this if possible?

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

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;

		  // string tables
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) {
	int bytesRead, idataSize, x;
	char buffer[800], user_answer;
	FILE * fp = 0;
	fp = fopen("PACKETS.txt", "r");

	if (fp == 0) 
	{
		cerr << "Error: Could not open file" << endl;
		exit(1);
	}
		// read in header
		bytesRead = fread(&header, 1, 16, fp);

		// read and display data portion of packet
		idataSize = header.totLength - 16; // calculate data size
		fread(buffer, 1, idataSize, fp); // read data into buffer

		// 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 < idataSize; i++) 
		{
			cout << (char)buffer[i];
		}
		
		cout << endl;
		system("pause");

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

struct 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;
};

int main()
{
    const char* const file_name = "PACKETS.txt" ;

    std::ifstream file( file_name, std::ios::binary ) ; // open for input in binary mode
    if( !file.is_open() )
    {
        std::cerr << "failed to open file\n" ;
        return 1 ;
    }

    header hdr ;
    const auto hdr_size = sizeof(hdr) ;
    if( file.read( reinterpret_cast<char*>( std::addressof(hdr) ), hdr_size ) &&
        file.gcount() == hdr_size )
    {
        // print out contents of header in a readable form

        const auto idata_size = hdr.totLength - 16 ;
        std::vector<char> buffer(idata_size) ;

        if( file.read( buffer.data(), buffer.size() ) && file.gcount() == idata_size )
        {
            // print out contents of the data buffer in a readable form
        }

        else
        {
           std::cerr << "failed to read data\n" ;
           return 1 ;
        }

    }

    else
    {
       std::cerr << "failed to read header\n" ;
       return 1 ;
    }
}
Cheers, this is fantastic! I was able to implement what I had into this and it works perfectly. Thanks again.
In the future is it possible to add a do while(y/n) to read multiple files?
Topic archived. No new replies allowed.