파일을 어휘로 분할하는 프로그램을 만들고 있지만 C에 익숙하지 않아 이상한 세분화 오류가 발생합니다. 다음 코드가 실행되고 인쇄는 :간단한 할당으로 인해 세그멘테이션 오류가 발생할 수 있습니까?
1
2
Segmentation Fault: 11
나는 오류를 세그멘테이션 다른 원인을 검토 한 결과 나는 내가 뭘 잘못 2. 후 배열에 아무것도 아니에요? 간단한 할당으로 세분화 오류가 발생할 수 있습니까?
#include <stdio.h>
#define CHAR_CODE 1
#define NUM_CODE 2
#define RESERVED_CHAR_CODE 3
#define SPACE_CODE 4
#define ERR_CODE 5
char token[16];
char line[128];
int lineLen = 0;
int counter = 0;
FILE *file;
void cleanToken(){
for(int i = 0 ; i < 16; i++){
token[i] = 0;
}
}
void cleanLine(){
for(int i = 0 ; i < 128; i++){
line[i] = 0;
}
}
void next(){
cleanToken();
printf("1\n");
char c;
if(counter == lineLen){
printf("2\n");
lineLen = 0;
printf("3");
cleanLine();
printf("4");
counter = 0;
while((c=getc(file)) != 10 && c != EOF){
if(c!=32){
line[lineLen] = c;
lineLen++;
}
printf("%d\n", lineLen);
}
printf("%s\n", line);
}
c = line[counter];
int type = getType(c);
while(getType(c)==type){
token[counter] = c;
counter = counter + 1;
c = line[counter];
if(c == EOF | c == 4 | type == RESERVED_CHAR_CODE){
break;
}
}
}
int getType(int c){
if((c > 96 & c < 123) | (c>64 & c < 91)){
return CHAR_CODE;
} else if(c < 58 & c > 46){
return NUM_CODE;
} else if(c == 32){
return SPACE_CODE;
} else if(c == 33 | c == 34 | (c > 36 & c < 44) | c == 45 | c == 47 | (c > 58 & c < 63)){
return RESERVED_CHAR_CODE;
} else {
return ERR_CODE;
}
}
int main(){
int c;
FILE* file;
file = fopen("test.txt", "r");
for(int i = 0; i < 10 ;i++){
next();
printf("%s\n", token);
}
return 0;
}
모든 경고가 활성화 된 상태에서 컴파일하십시오. –
'test.txt' 파일에는 무엇이 있습니까? –
코드를 들여 줘서 최소한의 예제를 제공하십시오 ... 그런 간단한 'printf' 문에 대한 seg 오류는 대개 토큰이 제대로 null 종료되지 않는다는 것을 의미합니다. –