【问题标题】:Why are atoi and strtol only returning the first number from my string most of the time?为什么 atoi 和 strtol 大多数时候只返回我的字符串中的第一个数字?
【发布时间】:2013-03-08 21:31:48
【问题描述】:

我正在尝试从具有如下输入的 c 文件中获取整数:

(0 3 200 3) (0 9 500 3) (98 20 500 3) (100 1 100 3) (100 100 500 3)

atoi 和 s 适用于括号后的第一个数字(我使用 while 循环和大于一位数的 strcat 数字)和任何只有一位数的数字,但它们只返回不是数字的第一个数字就在括号之后。

以下是该方法的代码:

 void allProcesses(FILE file, struct process processArray[]) {
char ch;
int number;
int a, b, c, io;
int arrayCount = 0;
int processIndex = 0;
char temp[1];

while ((ch = fgetc(&file)) != EOF) {

    if (isdigit(ch)) {

        char numberAppended[20] = "";

        while (isdigit(ch)) {
            temp[0] = ch;
            strcat(numberAppended, temp);
            ch = fgetc(&file);
        }

        char* end;
        number = (int)strtol(numberAppended, &end, 0);
        printf("The number is %d\n",number);
        int atoinum = atoi(numberAppended);



        switch (processIndex) {
            case 0:
                a = number;
                if (DEBUG == TRUE) {
                    printf("a = %c\n", a);
                    printf("NUmber a is %d\n", a);
                }
                processIndex++;
                break;
            case 1:
                b = number;
                if (DEBUG == TRUE) {
                    printf("b = %c\n", b);
                    printf("NUmber b is %d\n", b);
                }
                processIndex++;
                break;
            case 2:
                c = number;
                if (DEBUG == TRUE) {
                    printf("c = %c\n", c);
                    printf("NUmber c is %d\n", c);
                }
                processIndex++;
                break;
            case 3:
                io = number;
                if (DEBUG == TRUE) {
                    printf("io = %c\n", io);
                    printf("NUmber io is %d\n", io);
                }
                processIndex++;
                break;
            default:
                break;
        }
    }
    if (ch == ')') {
        processArray[arrayCount] = makeProcess(a, b, c, io);
        arrayCount++;
        processIndex = 0;
    }

}

}

【问题讨论】:

  • 顺便问一下temp 是什么?是否保证temp[1] 为0?
  • 在进入这段代码之前如何设置ch?问题可能出在您没有发布的周围循环中。
  • @user processIndex 在哪里更新?
  • char temp[1]; 使strcat(numberAppended, temp);temp[0] = ch; 之后出现未定义的行为。
  • @DanielFischer 是的未定义其行为

标签: c atoi strtol


【解决方案1】:

首先读取 cmets):

你已经根据你的代码声明了char temp[1]; 一个大小,它必须是2 的大小(否则由于内存溢出导致未定义的行为):

char temp[2];
while (isdigit(ch)) { // from `(` to `)`
   temp[0] = ch;   // should be a null terminated 
   temp[1] = '\0'; // add this step;
   strcat(numberAppended, temp); 
   ch = fgetc(&file);
}

第二:你的numberAppended被解析成一个字符串:"0 9 500 3"
你在打电话

number = (int)strtol(numberAppended, &end, 0);
                                      ^
                                      output argument

strtol 的语法:

long int strtol(const char *numberAppended, char **end, int base);  

在哪里

  • numberAppended: 是要转换成长整数的字符串。
  • end:指向一个指针,该指针将立即设置为字符串“numberAppended”中的长整数之后的字符

你要写这样的东西:(read cmets)

end = numberAppended;  // assign to first string
// in a loop {
number = (int)strtol(end, &end, 0);  // in loop end is input &end is output
printf("The number is %d\n",number); 
//}

我的以下代码将帮助您了解如何使用strtol()numberAppended 字符串中解析和提取数字:

#include <stdio.h>      /* printf */
#include <stdlib.h>     /* strtol */
int main (){
  char numberAppended[] = "2001 11 223   444   566";
  char * end;
  long int li;
  end =numberAppended;
  int base =10;
  int ele = 0;
  while(li=strtol (end, &end, base)){
     printf("%ld \n", li);
     ele += 1;
  }
  printf("\nNo of elements: %d", ele);
  return 0;
}

输出:

2001 
11 
223 
444 
566 

No of elements: 5

第三个:可能不是错误,但我找不到processIndexswitch(){} 之前在您的代码中更新的位置@..

【讨论】:

  • temp[1] 在此代码之前已经设置的几率高于平均水平。
  • @paxdiablo 是的,但它的代码不完整,出于你的原因,我在 mycode 中评论了should be a null terminated
  • for OP如果您需要更多帮助,请告诉我。还有文件读取错误!
  • 谢谢!!我忽略了这个问题,没想到这么简单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-04
  • 1970-01-01
相关资源
最近更新 更多