2010-01-22 170 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