2010-01-22 174 views
4

需要关于这个问题的一些建议,因为我正在苦苦挣扎,无法搞清楚。将Linux C Char阵列转换为Int

我有一个文件,在个人电脑上得到更新,表明系统运行和它运行的时间。我正在编写一个非常简单的Linux控制台应用程序(最终将成为一个nagios插件)。读取此文件并根据文件中找到的内容做出响应。

我是一个在Linux上编程和使用C的总新手,所以请耐心等待,如果你想解释任何答案,真的很感激。

基本上我想将一个包含5个字符的char数组转换为一个整数,但是数组中的第5个字符总是一个字母。所以在技术上,我想要做的是将数组中的前4个字符转换为整数...如何?我尝试过多种方式但没有成功,我的问题是,目前我对语言没有很好的把握,所以对于它可以做什么和不可以做什么都没有真正的想法。

这里是我的程序的源代码。

基本上buf数组中将会保持从文件中取出的字符串,就会是这个样子

3455Y(编号将是随机的,但总是4个字符长)。

很抱歉的代码格式不佳,但我无法得到爱情,也没有钱这个愚蠢的窗口中正确格式化....

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

} 
+0

要格式化代码,请使用窗口顶部的“10101”按钮在 – 2010-01-22 17:26:57

+0

中输入您的问题哈哈我知道有一个简单的答案,为什么我无法获得代码格式。谢谢:) – Kristiaan 2010-01-25 09:24:25

回答

3

您可以使用atoi

atoi需要一个char *参数并返回一个int。 如果字符串是空的,或第一个字符不是一个数字或一个减号,然后返回ATOI 0。如果遇到的atoi一个非数字字符时,它返回形成直到这一点

int num = atoi(buf); 
+0

虽然atoi有一些问题,人们已经提到上述,我只是真的需要它的一个非常简单的目的,将字符串的前4个字符转换为INT,所以即时选择此作为答案,因为它工作得很好。 感谢所有发布此问题解决方案的其他人。 – Kristiaan 2010-01-26 09:36:59

+0

为什么不用strtol,strtoul,strtoll或strtoull?他们有更好的错误检查。 – 2010-07-23 22:58:28

6

您可以使用sscanf会做的工作:

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

然后num应该保存文件中该行的数字。

1

数如果你想为一个字符串的前四个字符转换为整数做到这一点:

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

然后调用/测试这样

#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此功能,请不要使用atoi(...),因为它将任何字符串转换为整数,而不管其作为数字的有效性。 atoi("foo") === 0

0

这既是你的代码,因为我是能够从格式化恢复:

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

这是版本你指定什么呢:

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

与最大的问题你的代码是在每个分支中都带有返回值的if语句,以确保if语句执行后没有任何内容。

+0

嗨贾斯汀,感谢您指出在IF声明内的回报,虽然我意识到他们我的想法似乎把这个事实扼杀了..这将解释为什么以下代码没有工作... 感谢您的发现这个。 – Kristiaan 2010-01-26 15:13:04