嗨,很抱歉所有问题,但是当我运行此代码时,我的终端窗口上出现“分段错误(核心转储)”。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int usage (void) {
printf("Usage: head <file>\n");
printf(" Or: head <file> -n <number of characters>\n");
printf(" Or: head -n <number of characters> <file>\n");
return -1;
}
int main (int argc,char **argv) {
if ((argc != 2) && (argc != 4)) return usage();
char *fileName = argv[1];
int lineCount = 10;
FILE *src;
if ((argc == 4) && (strcmp(argv[1], "-n" != 0))){
fileName = argv[1];
lineCount = argv[3];
puts("-n in last position");
}else{
fileName = argv[3];
lineCount = argv[1];
puts("-n in first position");
}
if((src = fopen(fileName, "r")) == NULL){
puts("Can't open input file.");
exit(-1);
}
}
我很确定它与 fopen 函数有关,但我不确定为什么会发生这种情况。