Table of Contents
popen fgets 멈춤 현상
오류 : 멈춤
popen 이 EOF 에 도달해 있는 경우, fgets 는 멈춰버린다.
ls 명령이야 뭐라도 반환하니까 오류가 없겠지만…
cut 등과 같은 명령은 반환할 데이타가 없는 경우 문제가 발생한다.
fp = popen("ls -al", "r");
if (fp == NULL) {
return 0;
}
if (fgets(line, sizeof(line), fp) != NULL) {
line[strcspn(line, "\n")] = 0;
if (strlen(line) > 0) {
strcpy(result, line);
found = 1;
}
}
해결
feof 를 체크해 주면 된다.
fp = popen("ls -al", "r");
if (fp == NULL) {
return 0;
}
if (!feof(fp)) {
if (fgets(line, sizeof(line), fp) != NULL) {
line[strcspn(line, "\n")] = 0;
if (strlen(line) > 0) {
strcpy(result, line);
found = 1;
}
}
}