开发者

Convert Linux C Char Array to Int

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开发者_如何学JAVA, 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));

}


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.


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


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.


this is as much of your code as I was able to recover from the formatting:

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <time.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)); }

this is the version that does what you specified:

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <time.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; buf[4] = 0; } */ 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\n", test); printf("%d\n", atoi(buf)); }

The biggest problem with your code was the if statement with the returns in each branch, insuring that nothing after the if statement was ever executed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜