【问题标题】:Convert Linux C Char Array to Int将 Linux C 字符数组转换为 Int
【发布时间】:2025-12-27 11:35:10
【问题描述】:

在这方面需要一些建议,因为我正在苦苦挣扎,无法弄清楚。

我有一个文件在 PC 上得到更新,以指示系统运行和运行时间。我正在编写一个非常简单的 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));

}

【问题讨论】:

  • 要格式化代码,请使用您正在输入问题的窗口顶部的“10101”按钮
  • 哈哈,我知道为什么我无法格式化代码有一个简单的答案。谢谢:)

标签: c integer char


【解决方案1】:

您可以使用 sscanf 来完成这项工作:

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

那么 num 应该保存文件中该行的数字。

【讨论】:

    【解决方案2】:

    你可以使用atoi

    atoi 需要一个 char * 参数并返回一个 int。 如果字符串为空,或者第一个字符不是数字或减号,则 atoi 返回 0。如果 atoi 遇到非数字字符,则返回该点之前形成的数字

    int num = atoi(buf);
    

    【讨论】:

    • 虽然 atoi 存在人们上面提到的一些问题,但我只需要它来将字符串的前 4 个字符转换为 INT 的一个非常简单的目的,所以我选择这个作为答案它工作得很好。感谢所有发布此问题解决方案的人。
    • 为什么不使用 strtol、strtoul、strtoll 或 strtoull?他们有更好的错误检查。
    【解决方案3】:

    如果要将字符串的前四个字符转换为整数,请执行以下操作:

    #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.

    【讨论】:

      【解决方案4】:

      这是我能够从格式中恢复的尽可能多的代码:

      #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; 整数源; 诠释状态确定 = 0; 诠释状态警告 = 1; int STATE_CRITICAL = 2; 诠释状态未知= 3; int system_paused = 0;

      字符缓冲区[5]; int testnumber;

      if((fd = open(argv[1], O_RDONLY)) == -1) { printf("打开失败:%s", argv[1]); 返回状态_未知; } 别的 { nRead = read(fd, buf, 5); } 关闭(来源);

      如果 (buf[4] == 'P') { printf("软件暂停"); 返回状态警告; } 别的 { 返回状态确定; } time_t ltime; /* 日历时间 / 结构 tm Tm; ltime=时间(NULL); / 获取当前校准时间 */ tm=本地时间(&ltime);

      int 测试; 测试 = Tm->tm_hour + Tm->tm_min; printf("%d", 测试); 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; 整数源; 诠释状态确定 = 0; 诠释状态警告 = 1; int STATE_CRITICAL = 2; 诠释状态未知= 3; int system_paused = 0;

      字符缓冲区[5]; int testnumber;

      if((fd = open(argv[1], O_RDONLY)) == -1) { printf("打开失败:%s", argv[1]); 返回状态_未知; } 别的 { nRead = read(fd, buf, 5); } 关闭(来源);

      如果 (buf[4] == 'P') { printf("软件暂停"); 返回状态警告; }/* 别的 { 返回状态确定; 缓冲区 [4] = 0; } */ time_t ltime; /* 日历时间 */ 结构 tm *Tm; ltime=时间(NULL); /* 获取当前校准时间 */ tm=本地时间(&ltime);

      int 测试; 测试 = Tm->tm_hour + Tm->tm_min; printf("%d\n", 测试); printf("%d\n", atoi(buf)); }

      您的代码最大的问题是 if 语句在每个分支中都有返回值,从而确保在 if 语句之后没有任何内容被执行。

      【讨论】:

      • 嗨,贾斯汀,感谢您指出 IF 语句中的返回值,尽管我知道它们,但我的大脑似乎忽略了这一事实......这可以解释为什么下面的代码都不起作用。 .. 感谢您发现这一点。