tags:

views:

549

answers:

4

Hi Guys, need some advice on this one as im struggling abit and cannot figure it out.

i have a file that gets updated on a PC to indicate a system ran and what time it ran. i am writing a very simple linux console app (will eventually be a nagios plugin). that reads this file and responds depending on what it found within the file.

i am a total newbie to programming on Linux and using C so please be patient and if you would explain any answers it would really be appreciated.

basically i want to convert a char array containing 5 characters into an integer, however the 5th char in the array is always a letter. so technically all i want to-do is convert the first 4 chars in the array to a integer... how?? ive tried multiple ways with no success, my problem is that presently i do not have a good grasp of the language so have no real ideas on what it can and cannot do.

here is the source to my program.

basically the buf array will be holding a string taken from the file that will look something like this

3455Y (the number will be random but always 4 chars long).

Sorry for the poor formatting of the code, but i cannot get this stupid window for love nor money to format it correctly....

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  STATE_OK = 0;
  int  STATE_WARNING  = 1;
  int  STATE_CRITICAL = 2;
  int  STATE_UNKNOWN  = 3;
  int  system_paused  = 0; 

  char buf[5]; 
  int  testnumber;

  if((fd = open(argv[1], O_RDONLY)) == -1)
    {
      printf("failed open : %s", argv[1]);
      return STATE_UNKNOWN;
    }
      else
    {
      nRead = read(fd, buf, 5);
    }

  close(source);

  if (buf[4] == 'P')
    {
      printf("Software Paused");
      return STATE_WARNING;
    }
      else
    {
      return STATE_OK;
    }
    time_t ltime; /* calendar time */  
    struct tm *Tm;
    ltime=time(NULL); /* get current cal time */  
    Tm=localtime(&ltime);


    int test;
    test = Tm->tm_hour + Tm->tm_min;
    printf("%d", test);

    printf("%d", strtoi(buf));

}
+3  A: 

You can use sscanf to do the job:

int num = 0;
sscanf(buf, "%4d", &num);

Then num should hold the number from the line in the file.

pib
A: 

You can use atoi

atoi requires one char * argument and returns an int. If the string is empty, or first character isn't a number or a minus sign, then atoi returns 0.If atoi encounters a non-number character, it returns the number formed up until that point

int num = atoi(buf);
Scott
although atoi has some problems that people have mentioned above, i only really need it for a very simple purpose of converting the first 4 chars of a string into an INT, so im choosing this as the answer as it works very well.thanks to everyone else who posted a solution to this problem.
Kristiaan
Why not strtol, strtoul, strtoll or strtoull? They have much better error checking.
Joe D
+1  A: 

if you want to convert the first four characters of a string to an integer do this:

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>

uint8_t convertFirstFourChars(char * str, uint32_t *value){
  char tmp[5] = {0};
  strncpy((char *) tmp, str, 4);
  *value = strtoul(tmp);
  return errno;
}

then call / test this function like this

#include <stdint.h>
#include <stdio.h>

int main(int argc, char **argv){

  char test1[5] = "1234A";
  char test2[5] = "ABCDE";

  uint32_t val = 0;
  if(convertFirstFourChars((char *) test1, &val) == 0){
    printf("conversion of %s succeeded, value = %ld\n", test1, val);
  }
  else{
    printf("conversion of %s failed!\n", test1);
  }

  if(convertFirstFourChars((char *) test2, &val) == 0){
    printf("conversion succeeded of %s, value = %ld\n", test2, val);
  }
  else{
    printf("conversion of %s failed!\n", test2);
  }

  return 0;
}

FWIW, don't use atoi(...) because it converts any string to an integer regardless of its validity as a number. atoi("foo") === 0.

vicatcu
A: 
Justin Smith
hi Justin, thanks for pointing out the returns within the IF statement, although i was aware of them my mind seemed to blank out that fact.. which would explain why none of the code below was working...thanks for spotting this.
Kristiaan