C 문자열 입력 EOF 끝내기

익명 사용자의 이미지

#include
#include
int stdin_count(char line[][100])
{
int i;
for(i = 0;i<100;i++){
fgets(line[i], 100, stdin);
if(line[i][0]==EOF)
return 0;

// printf("i is %d\n",i);
}
return 0;
}

int stdout_print(char line[][100])
{
int i;
for(i=0;i<100-1;i++){
printf("%s",line[i]);
if(line[i][0]==EOF)
return 0;

}
return 0;
}

int main(int argc, char * argv[])
{
char line[100][100];

stdin_count(line);
stdout_print(line);

return 0;

}
=====================
EOF(ctrl +D)입력 받아 중간에 끊고 싶은데
line[i][0]==EOF //이 방법이 아닌거 같습니다.

어떻게 수정해야 하나요???

 의 이미지

http://en.cppreference.com/w/c/io/fgets

reference wrote:
Return value:
str on success, null pointer on failure.
If the failure has been caused by end-of-file condition, additionally sets the eof indicator (see feof()) on stream. The contents of the array pointed to by str are not altered in this case.
If the failure has been caused by some other error, sets the error indicator (see ferror()) on stream. The contents of the array pointed to by str are indeterminate (it may not even be null-terminated).
익명 사용자의 이미지

출력함수의 조건은 어떻게 되나요....????

익명 사용자의 이미지

일단 배열 길이를 측정해 그만큼 출력하는 것으로 해결했습니다.
이 방법 말고 다른 방법도 궁금하군요...ㅠ

raymundo의 이미지

출력 함수의 조건이란 질문이 무슨 뜻인가 했는데, 총 100라인 중에서 몇 라인까지 입력을 받았는지 어떻게 확인해서 그만큼만 출력할 수 있느냐라는 뜻이라면

stdin_count 가 어차피 int 형을 반환할 거라면 그냥 0을 반환할 게 아니라 i 값을 반환하게 하고 그 값을 다시 출력함수에 전달해주면 되겠네요.

아니면 main 에서 line 배열을 전부 0으로 초기화한다면 지금처럼 각 라인의 첫번째 글자 (line[i][0]) 이 0인지 검사하는 것으로도 괜찮을 것 같고요.

좋은 하루 되세요!