2. プログラムを実行し、キーボード入力待ちにし、キーボードが入力された ら、プログラム実行からキーボード入力までの経過時間を表示するプログラ ム を作成せよ。 #include <stdio.h> #include <time.h> main() { time_t timer[3]; timer[0] = time(NULL); printf("Input Something -> "); fflush(stdout); getchar(); timer[1] = time(NULL); printf("%d sec\n",timer[2]=timer[1]-timer[0]); } 3. ファイルを読み込んで、その内容を 表示するプログラムを作成せよ。 #include <stdio.h> #include <stdlib.h> #include <errno.h> if(fd<0){ perror("File Name"); exit(EXIT_FAILURE); } #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> main(narg,arg) int narg; char **arg; { int fd; char ch; fd=open(arg[1], O_RDONLY); while(read(fd,&ch,sizeof(char))) printf("%c",ch); close(fd); } 4. キーボードから文字を入力して、その内容を ファイルに書き込むプログラムを作成せよ。 fd=open(arg[1],O_WRONLY|O_APPEND |O_CREAT); if(fd<0){ perror("File Name"); exit(EXIT_FAILURE); } #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> while(1){ ch=getchar(); main(narg,arg) int narg; char **arg; { if(ch==EOF) break; write(fd,&ch,sizeof(char)); int fd; char ch; } close(fd); }
© Copyright 2025 ExpyDoc