Convert Linux C Char Array to Int

you need to consult a bit with this, since I am afraid of abit and cannot figure it out.

I have a file that is updated on a PC to indicate that the system is running and at what time it starts. I am writing a very simple Linux console application (ultimately it will be the nagios plugin). which reads this file and responds depending on what it found in the file.

I am a complete newbie in Linux programming and using C, so please be patient and if you explain all the answers, it will be really appreciated.

Basically, I want to convert a char array containing 5 characters to 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 characters into an array into an integer ... how? ive tried several methods without success, my problem is that at the moment I don’t have a good understanding of the language, so I don’t have real ideas about what it can and cannot do.

here is the source of my program.

basically, the buf array will contain a string taken from a file that will look something like this.

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

Sorry for the poor formatting of the code, but I can’t get this stupid window for love or 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)); } 
+4
source share
4 answers

You can use atoi

atoi requires a single char * argument and returns int. If the string is empty or the first character is not a number or a minus sign, then atoi returns 0. If atoi meets a non-digital character, it returns a number formed up to this point

 int num = atoi(buf); 
+3
source

You can use sscanf to do the job:

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

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

+6
source

if you want to convert the first four characters of a string to an integer, follow these steps:

 #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 as follows

 #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, do not use atoi(...) , because it converts any string to an integer regardless of its reality as a number. atoi("foo") === 0 .

+1
source

this is the same code as i was able to recover from 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 is the if statement with return values ​​in each branch, ensuring that nothing is executed after the if statement is executed.

0
source

Source: https://habr.com/ru/post/1299129/


All Articles