I am trying to read in data from a text file (the time). and convert that into something that can be DiffTime'ed to the current system time.
I am now so close to getting this working correctly, I can taste it but I am stuck with an issue I cannot work out. (I have a very basic grasp of the C language).
this program reads in the data from the text file, splits it into two char arrays, and then tries to use atoi
to convert this into an integer. However, I am getting problems with the second atoi
call.
From the bottom two printf
statements, I should be getting:
12 34
but for some reason i am getting something along these lines.
12 3412
I presume that atoi
is overunning the boundries of one of the char arrays or my char arrays are too long or too short. Either way, I cannot fathom what is going on.
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define COPYMODE 0644
int main (int argc, char *argv[]){
int i, nRead, fd;
int source;
int ihour;
int imin;
int STATE_OK = 0;
int STATE_WARNING = 1;
int STATE_CRITICAL = 2;
int STATE_UNKNOWN = 3;
/* indicates if system is paused 1 = System is paused, 0 = System running */
int system_paused = 0;
char filebuf[5];
char hourbuf[2];
char minbufer[2];
if((fd = open(argv[1], O_RDONLY)) == -1)
{
printf("failed open : %s", argv[1]);
}
else
{
nRead = read(fd, filebuf, 5);
}
close(source);
printf("filebuffer %s\n", filebuf);
hourbuf[0] = filebuf[0];
hourbuf[1] = filebuf[1];
printf("Hour Buffer %c%c\n", hourbuf[0],hourbuf[1]);
minbufer[0] = filebuf[2];
minbufer[1] = filebuf[3];
printf("Min Buffer %c%c\n", minbufer[0],minbufer[1]);
imin = atoi(minbufer);
ihour = atoi(hourbuf);
printf("hour as int %d\n", ihour);
printf("min as int %d\n", imin);
return 0;
}