Problem with binary

Hi Guys, I'm in my 2nd year of college and got a task of our lecturer to write a program that will read in readings from a weather station and store them to a binary file. I have that part done. The second part of the program is :

"Write a program that will open and read the file generated by the previous program and will allow the user to query the file by date to view monthly averages.
1. Display a menu with the following options:
- Load file
- Get Weather by date
- View monthly average
- Export to Excel
- Exit
2. Load file option must load the entire file into memory
3. Get weather by date allows the user to enter a date and view the weather reading for that date
4.Export data from binary file to Microsoft Excel
5. The menu must run until the user quits the application."

So Far I have done parts 1 and 2 and i'm stuck on part 3 don't even know how to start. I was thinking of reading the binary file as an array and then making reading the input date in to 3 single integers and comparing them with a string.

The code below, Im not attaching first program as its not needed.

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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <iostream>


using namespace std;
struct result
{

	int temp;
	int wind;
	time_t time;

};

FILE *awind;

int main()
{
	int option; // user's entered option will be saved in this variable
	char ch;
	result *readings;
	do //do-while loop starts here.that display menu again and again until user select to exit program
	{
		//Displaying Options for the menu
		printf("1) Load a file ");
		printf("2) Get weather by date ");
		printf("3) View monthly average");
		printf("4) Export to excel");
		printf("5) Exit Program ");
		//Prompting user to enter an option according to menu
		printf("Please select an option : ");
		scanf_s("%i", &option);  // taking option value as input and saving in variable "option"

		if (option == 1) // Checking if user selected option 1
		{
			fopen_s(&awind, "readings.bin", "rb");
			if (awind == NULL)
			{
				perror("Error while opening the file.\n");
				exit(EXIT_FAILURE);
			}

			fseek(awind, 0L, SEEK_END);
			int sz = ftell(awind) / sizeof(result);


			fseek(awind, 0L, SEEK_SET);

			readings = (result*)malloc(sz * sizeof(result));
			fread(readings, sizeof(result), sz, awind);

		}
		else if (option == 2) // Checking if user selected option 2
		{
			int day, month, year;
			printf("Please enter the day (dd): ");
			scanf_s("%d", &day);
			printf("Please enter the month (mm): ");
			scanf_s("%d", &month);
			printf("Please enter the year (yyyy): ");
			scanf_s("%d", &year);



			time_t theTime;
			tm *newTime;
			theTime = time(NULL);
			printf("the time is:%d \n", theTime);
			newTime = localtime(&theTime);
			printf("the date is:%i%i%i" newTime->tm_mday->, newTime->tm_mon + 1, 1900 + newTime->tm_year);


		}
		else if (option == 3) // Checking if user selected option 3
		{
			printf("retrieveing averages:");
		}
		else if (option == 4) // Checking if user selected option 4
		{
			printf("exporting file to excel");
		}
		else //if user has entered invalid choice (other than 1,2,3 or 4)
		{
			//Displaying error message
			cout << "Invalid Option entered" << endl;
		}
	} while (option != 4);  //condition of do-while loop

	return 0;
}


Thanks for any help in advance.
Last edited on
Topic archived. No new replies allowed.