tags:

views:

159

answers:

4

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;
}
+10  A: 

You need to null terminate the strings. Make hourbuf and minbufer 1 longer and put a 0 at the end before calling atoi. The two buffers are adjacent on the stack, so reading minbufer scans into hourbuf as well.

Mark Wilkins
+1, because you also gave a good explanation for the strange '3412' output.
Alexander Gessler
my brain was telling me this, but i could not work out how to stop it doing it, which was driving me nuts. i came from a programming background where there was no "null termination" so i keep forgetting it.... :)thanks for the reply.
Kristiaan
A: 

You forgot to null terminate:

minbufer[2] = '\0'
Alex Brown
Your idea is right, so I'm not downvoting. However, I suspect you got a downvote because you didn't point out that minbufer needs to have 3 chars ..
GreenMatt
I considered it, but I decided that the coder REALLY should be able to work that out hisself.
Alex Brown
your correct as well Alex :) i would have worked that one out, proberbly not as quickly as you guys but it would have hit me. this language is a real learning curve for me :)cheers for the reply.
Kristiaan
+7  A: 

You forgot to terminate your char buffers with zero.

char hourbuf[2];
char minbufer[2];

Should be

char hourbuf[3];
char minbufer[3];

hourbuf[2] = '\0';
minbufer[2] = '\0';
Alexander Gessler
+1  A: 

atoi assumes its argument is a C-style string, which means it must contain a trailing nul character.

Your handcrafted pseudostrings don't ensure this.

A solution would be to enlarge them and set the last element to '\0:

char hourbuf[3];
int hour;
...
hourbuf[0] = filebuf[0];
hourbuf[1] = filebuf[1];
hourbuf[2] = '\0';
hour = atoi(hourbuf);
bltxd