这个程序怎么上来就崩溃了
我就是想让它在命令行里输入参数然后让它打印指定文件里的最后指定行,可是一运行就崩溃了,能帮我看看错在哪里吗?另外,像这种一上来就崩溃的程序该怎么调试?我把breakpoint设在最前面也不行。谢谢!
#include <stdio.h>
#include <stdlib.h>
main (int argc, char *argv[]) {
FILE *input;
int lines2Print, totalLines = 0, i;
char *fileName, *currentLine;
if (argc == 3) {
fileName = argv[2];
lines2Print = 10;
} else if (argc == 4) {
fileName = argv[3];
lines2Print = - atoi (argv[2]);
} else {
fprintf (stderr, "input error, it should be: c0805 tail [-n] filename.xxx\n ");
return 0;
}
if ((input = fopen (fileName, "r ")) == NULL ) {
fprintf (stderr, "cannot open file %s\n ", fileName);
return 0;
}
while (fgets (currentLine, 128, input) != NULL) {
totalLines++;
}
fseek (input, 0l, SEEK_SET);
for (i = 1; i <= totalLines - lines2Print; i++) {
fgets (currentLine, 128, input);
}
while (fgets (currentLine, 128, input) != NULL) {
printf ( "%s\n ", currentLine);
}
fclose (input);
}
[解决办法]
currentLine没有分配内存.