
Patrick B. answered 04/03/21
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
typedef struct _TTimeRec
{
int min;
int sec;
long secs;
} * TTimeRec;
typedef struct _TTimeRec TimeRec;
#define TIME_REC_SIZE (sizeof(struct _TTimeRec))
int CompareTimeRec( const void * rec1, const void * rec2)
{
TTimeRec timeRecPtr1 = (TTimeRec) rec1;
TTimeRec timeRecPtr2 = (TTimeRec) rec2;
int iReturn=0;
long timeSecs1 = timeRecPtr1->secs;
long timeSecs2 = timeRecPtr2->secs;
if (timeSecs1 != timeSecs2)
{
iReturn = (timeSecs1 > timeSecs2) ? 1 : -1;
}
return(iReturn);
}
Go()
{
vector<TimeRec> vecTimeRecs;
FILE * fptr = fopen("C:\\users\\pdiru\\desktop\\timerecs.dat","r");
if (fptr != NULL)
{
struct _TTimeRec curTimeRec;
while (!feof(fptr))
{
fscanf(fptr,"%d %d \n",&curTimeRec.min,&curTimeRec.sec);
cout << "read : " << curTimeRec.min << " : " << curTimeRec.sec << endl;
if (feof(fptr)) { break; }
curTimeRec.secs = 60*curTimeRec.min + curTimeRec.sec;
vecTimeRecs.push_back(curTimeRec);
}
int N = vecTimeRecs.size();
TTimeRec timeRecs =(TTimeRec) malloc(N * TIME_REC_SIZE);
for (int iLoop=0; iLoop<N; iLoop++)
{
timeRecs[iLoop] = (TimeRec)vecTimeRecs[iLoop];
}
qsort(timeRecs,N,TIME_REC_SIZE,CompareTimeRec);
for (int iLoop=0; iLoop<N; iLoop++)
{
cout << timeRecs[iLoop].min << " " << timeRecs[iLoop].sec << endl;
}
free(timeRecs);
}
else
{
cout << " ERROR OPENING and/or READING DATA FILE " << endl;
}
}
int main()
{
Go();
}