-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathex6.c
39 lines (31 loc) · 958 Bytes
/
ex6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* 6. Write a program that displays the contents of a file at the terminal 20
* lines at a time. At the end of each 20 lines, have the program wait for a
* character to be entered from the terminal. If the character is the letter q,
* the program should stop the display of the file; any other character should
* cause the next 20 lines from the file to be displayed.
*
* By Faisal Saadatmand
*/
#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 1000
#define MAXLINES 20
#define NAME_MAXLEN 64
int main(void)
{
int i;
char fileName[NAME_MAXLEN], line[MAXLEN];
FILE *inFile;
printf("Enter the name of file to read: ");
scanf("%63s", fileName);
if (!(inFile = fopen(fileName, "r"))) {
fprintf(stderr, "Can't open %s for reading.\n", fileName);
exit(EXIT_FAILURE);
}
while (getchar() != 'q')
for (i = 0; i < MAXLINES; ++i)
fprintf(stdout, " %s\n", fgets(line, MAXLEN, inFile));
fclose(inFile);
exit(EXIT_SUCCESS);
}