analyzing metadata in a binary file

Does anyone know of the location of a tutorial that can show me how to read from a binary file and determine what (of three different types) of data are present within the file? I have scoured the internet back and forth and have found nothing. I am doing a project that should read a binary file and then give a 1 or a 0 based on the presence of three types (in this case height, salinity and temp of the ocean) of data within. I've been given the code to write the binary file, and also have written my code to where it will work if I tell it what types of data are present from the command line. I, for the life of me don't know where to start on analyzing binary values. Any help at all would be appreciated. Below is the code I was given to create the binary file. When you run from the command line, you type in the program name followed by the three flags and then a data size, for instance 1 1 0 50, then my program should be able to read that binary file, and spit back the same 1 1 0 50

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
  #include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <stdlib.h>
#include <time.h>

using namespace std;

int createTestData(int flags[], int dataSize)
  {
  ofstream binary_file("data.in", ios::out|ios::binary);

  if(!binary_file)
    {
    cout << "The file data.in could not open for a binary write.\n" << endl;
    return(1);
    }
  else if(!binary_file.write(reinterpret_cast<char *>(flags),sizeof(int)*3))
    {
    cout << "Write failure 1 in createTestData().\n" << endl;
    return(1);
    }
  else if(!binary_file.write(reinterpret_cast<char *>(&dataSize),sizeof(int)))
    {
    cout << "Write failure 2 in createTestData().\n" << endl;
    return(1);
    }

  int val;

  for(int i = 0; i < 3; i++)
    {
    if(flags[i] == 1)
      {
      for(int j = 0; j < dataSize; j++)
	{
	val = rand();

	if(!binary_file.write(reinterpret_cast<char *>(&val),sizeof(int)))
	  {
  	  cout << "Write failure 3 in createTestData().\n" << endl;
	  return(1);
	  }
	}
      }
    }

  binary_file.close();
  return(0);
  }

//-----------------------------------------------------------------------------

int main(int argc, char *argv[])
  {
  int dataFlags[3];
  int dataSize = 0;

  if(argc != 5)
    {
    cout << "\nUsage: " << argv[0] << " sstFlag sFlag sshFlag dataArraySize\n" << endl;
    return(1);
    }
  else
    {
    dataFlags[0] = atoi(argv[1]);
    dataFlags[1] = atoi(argv[2]);
    dataFlags[2] = atoi(argv[3]);

    dataSize = atoi(argv[4]);

    createTestData(dataFlags, dataSize);
    }

  return(0);
  }
Topic archived. No new replies allowed.