Kristiaan
Kristiaan

Reputation: 253

Why is atoi returning random numbers?

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;
}

Upvotes: 0

Views: 1304

Answers (4)

bltxd
bltxd

Reputation: 9113

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);

Upvotes: 1

Alexander Gessler
Alexander Gessler

Reputation: 46607

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';

Upvotes: 7

Alex Brown
Alex Brown

Reputation: 42872

You forgot to null terminate:

minbufer[2] = '\0'

Upvotes: -1

Mark Wilkins
Mark Wilkins

Reputation: 41222

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.

Upvotes: 10

Related Questions