一定時間経過後にループを終了させたいのですが
Posted: 2011年1月12日(水) 16:33
プログラム実行開始から一定時間に入力された文字数なり行数なりをカウントさせたいのですが、
以下の書き方では、まったくキーをさわらなければ永遠にプログラムが終了しません。
どうすれば、一定時間後にループを抜けれるのでしょうか?
#include <stdio.h>
#include <time.h>
int main(){
int count;
char line_buf[1024];
count = 0;
while (fgets(line_buf, 1024, stdin) != NULL && (clock()/CLOCKS_PER_SEC) < 20){
fputs(line_buf, stdout);
count++;
}
printf("%d\n",count);
}
以下の書き方では、まったくキーをさわらなければ永遠にプログラムが終了しません。
どうすれば、一定時間後にループを抜けれるのでしょうか?
#include <stdio.h>
#include <time.h>
int main(){
int count;
char line_buf[1024];
count = 0;
while (fgets(line_buf, 1024, stdin) != NULL && (clock()/CLOCKS_PER_SEC) < 20){
fputs(line_buf, stdout);
count++;
}
printf("%d\n",count);
}