ATOI()导致“意外退出/丢失连接”

时间:2014-02-01 21:46:51

标签: objective-c c reference xcode5

我正在尝试从Xcode 5运行一个简单的命令行程序。当我运行它时,我在shell中获得用户输入提示,但随后它崩溃并给我错误Exited Unexpectedly / Lost Connection

这是我的代码。如果我错过了库参考,或者我的代码中有什么问题,你能告诉我吗?

我还想在Build Phases - >中提及这一点。 Link Binary With Libraries,我引用了libreadline.dylib

#include <stdio.h>
#include <stdlib.h>

#import <readline/readline.h>
//#import <stdio.h>

int main(int argc, const char * argv[])
{
    printf("Where should I start counting? ");

    int i;
    int countFrom = atoi(readline(NULL));

    for (i = countFrom; i >= 0; i -= 3) {
        printf("%d\n",i);

        if (i % 5 == 0){
            printf("Found one!\n");
        }

    }

    return 0;
}

1 个答案:

答案 0 :(得分:1)

陷入类似的问题。发现用小键盘命中“enter”键会导致readline(NULL)崩溃。通过点击“返回”键来解决这个问题。这是使用Mac附带的键盘。希望这有帮助!