전역 포인터 변수를 정의하려고합니다.이 포인터 변수는 아래와 같이 main 함수에서 실제로 설정할 수 있습니다. 그러나이 후에 outputName
을 사용하려고 할 때마다 세그먼트 오류가 발생합니다. 나는 아마도 처음에 NULL
과 같은 포인터를 설정하는 것과 관련이 있다는 것을 알고 있습니다 ... 어떻게하면 메인에 설정되어있는 전역 포인터를 가질 수 있었는지에 대한 도움이 매우 도움이 될 것입니다! 다음은 나에게 오류를주고 내 코드의 일부입니다글로벌 포인터를 사용한 세그먼트 오류
char* outputName=NULL;
int isNumber(char number[]){
int i;
if (number[0]=='-'){
i=1;
}
while(number[i] != '\0'){
if (!isdigit(number[i])){
return 0;
}
i++;
}
return 1;
}
void catcher(int signo) {
printf("The program is exiting early");
remove(outputName);
exit(1);
}
int main(int argc, char *argv[]) {
if (argc != 4){
fprintf(stderr,"Incorrect number of arguments, must supply three.\n");
exit(1);
}
char* inputName = argv[1];
outputName=argv[2];
signal(SIGINT, catcher);
int result=isNumber(argv[3]);
if (result==0){
fprintf(stderr, "Invalid maximum line length, please enter an integer\n");
exit(1);
}
int maxChars= (atoi(argv[3]))+1;
if ((maxChars-1)<1){
fprintf(stderr, "Invalid third maximum line length, please enter an integer greater than zero\
.\n");
exit(1);
}
FILE* inFile = fopen(inputName, "r");
if (inFile==NULL){
fprintf(stderr,"Error while opening %s.\n", inputName);
exit(1);
}
FILE* outFile= fopen(outputName, "w");
if (outFile==NULL){
fprintf(stderr,"Error while opening %s.\n", outputName);
exit(1);
}
char line[maxChars];
int done=0;
while (!done){
char *readLine=fgets(line,maxChars,inFile);
if (readLine==NULL){
if (errno==0){
done=1;
}
else{
fprintf(stderr, "Error when reading line from input file");
exit(1);
}
}
int len=strlen(line);
if (line[len-1]!='\n'){
line[len]='\n';
line[len+1]='\0';
char current=' ';
while (current != '\n'){
current=getc(inFile);
}
}
if (!done){
fputs(line, outFile);
if (errno!=0){
fprintf(stderr, "Error when writing line to output file");
exit(1);
}
}
}
return 0;
}
'main()'에 몇 개의 인수를 전달합니까? 당신은'argv [2]'의 값이 무엇인지보기 위해 기본적인 디버깅을 했습니까? 'argc> = 3'을 확인 했습니까? – John3136
이 네 개의 인수를 전달하면 yes가 모든 오류 검사를 완료하여 are의 값이 올바른지 확인하십시오. – liverr1818
위의 코드 다음에'outputName'이 유효하다면 문제는 다른 곳에서 발생합니다 ... – John3136