我试图创建一个可以获得输入的程序,如
1 833N619DL ABQ 26 204 214 1250
1 1125N660DL ABQ 18 211 204 1250
1 1589N678DL ABQ 23 206 212 1250
2 833N659DL ABQ 18 204 194 1250
2 1125N656DL ABQ 23 211 210 1250
2 1589N665DN ABQ 19 206 212 1250
3 833N686DA ABQ 23 204 204 1250
3 1125N692DL ABQ 27 211 216 1250
3 1589N635DL ABQ 26 206 210 1250
4 833N688DL ABQ 21 204 206 1250
4 1125N660DL ABQ 18 211 221 1250
4 1589N631DL ABQ 28 206 209 1250
此数据和输出只输入输入的数字(不包括数字和字母混合的列)
这是我的程序
#include <stdio.h>
#include <ctype.h>
#define MAXLINE 10000
char *itoa(int i); //convert integer to char array
main() {
int temp= 0, i = 0, c, j = 0;
char *a[MAXLINE]; //pointer array with uninitialized pointers
while((c = getchar()) != EOF)
if(isdigit(c))
temp = 10 * temp + c - '0';
else if(!isalpha(c)) {
a[i] = itoa(temp);
++i;
if(c == '\n') {
*a[i] = '\n';
j++;
}
}
printf("Data is\n");
for(int n = 0; n <= j; n++)
printf("%s\n", a[n]);
}
char *itoa(int i) {
char *s;
while(i % 10 != 0) {
*s++ = i / 10; //save to s
i = i % 10; //removed saved part from i
}
return s;
}
我编译了它并提供了上面的数据。我应该按下控制器-D来告诉程序我已完成输入(EOF),但是程序响应了一个分段故障11,然后,终端开始接受我的输入,好像我这样写了:
urw7RS:Desktop urw7rseeh8fr$ 1 1125N660DL ABQ 18 211 204 1250
终端告诉我,命令没有找到。 这是输出:
urw7RS:Desktop urw7rseeh8fr$ ./a.out
1 833N619DL ABQ 26 204 214 1250
1 1125N660DL ABQ 18 211 204 1250
1 1589N678DL ABQ 23 206 212 1250
2 833N659DL ABQ 18 204 194 1250
2 1125N656DL ABQ 23 211 210 1250
2 1589N665DN ABQ 19 206 212 1250
3 833N686DA ABQ 23 204 204 1250
3 1125N692DL ABQ 27 211 216 1250
3 1589N635DL ABQ 26 206 210 1250
4 833N688DL ABQ 21 204 206 1250
4 1125N660DL ABQ 18 211 221 1250
4 1589N631DL ABQ 28 206 209 1250
Segmentation fault: 11
urw7RS:Desktop urw7rseeh8fr$ 1 1125N660DL ABQ 18 211 204 1250
-bash: 1: command not found
urw7RS:Desktop urw7rseeh8fr$ 1 1589N678DL ABQ 23 206 212 1250
-bash: 1: command not found
urw7RS:Desktop urw7rseeh8fr$ 2 833N659DL ABQ 18 204 194 1250
-bash: 2: command not found
urw7RS:Desktop urw7rseeh8fr$ 2 1125N656DL ABQ 23 211 210 1250
-bash: 2: command not found
urw7RS:Desktop urw7rseeh8fr$ 2 1589N665DN ABQ 19 206 212 1250
-bash: 2: command not found
urw7RS:Desktop urw7rseeh8fr$ 3 833N686DA ABQ 23 204 204 1250
-bash: 3: command not found
urw7RS:Desktop urw7rseeh8fr$ 3 1125N692DL ABQ 27 211 216 1250
-bash: 3: command not found
urw7RS:Desktop urw7rseeh8fr$ 3 1589N635DL ABQ 26 206 210 1250
-bash: 3: command not found
urw7RS:Desktop urw7rseeh8fr$ 4 833N688DL ABQ 21 204 206 1250
-bash: 4: command not found
urw7RS:Desktop urw7rseeh8fr$ 4 1125N660DL ABQ 18 211 221 1250
-bash: 4: command not found
urw7RS:Desktop urw7rseeh8fr$ 4 1589N631DL ABQ 28 206 209 1250
-bash: 4: command not found
urw7RS:Desktop urw7rseeh8fr$
我做错了什么?
答案 0 :(得分:0)
似乎终端是解释你的数据而不是处理它,所以它试图像执行一个命令那样执行每一行,并且没有这样的命令作为数字引导你文件的每一行。您确定要执行程序而不是包含数据的文件吗?
此外,您的所有char*
字符串指针都未分配,因此您获得了Segmentation fault