Segmentation fault
호습다
질문 제목 : segmentation fault
이클립스에서 실행하면 멀쩡하게 실행되는 소스가 유닉스에서 실행하니 segmentation fault 발생합니다.
소스를 주석처리하면서 재실행해보니 fgets에서 에러가 발생하는 것 같은데 도대체 이유를 모르겠네요 ㅠㅠ
질문 내용 : 소스 첨부합니다.
#include stdio.h
#include stdlib.h
#include unistd.h
#include errno.h
#include string.h
#define maxbuflen 100
void fileopen(char* filename, char (*book)[2][maxbuflen]){
file* pfile;
char buf[maxbuflen];
char* token;
int i=0, j=0;
pfile = fopen(filename, r); // open the input file
while(1){
if(fgets(buf, maxbuflen, pfile) == null)
break;
token = strtok(buf, #);
while(token){
strcpy(book[i][j++], token);
token = strtok(null, #);
}
}
i++;
fclose(pfile); // close the input file
}
int main()
{
char book[5][2][maxbuflen];
fileopen(library1.txt, book);
}
-
진솔
프로그램이 좀 불안해 보입니다. FileOpen함수 내에서 book의 배열 첨자들이 main함수 book 배열 갯수들을 넘지 않도록 제한해 보세요. i 5, j 2 가 되도록 말이죠.