-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex5_7.c
68 lines (51 loc) · 1.06 KB
/
ex5_7.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <string.h>
#define MAXLINES 500
#define MAXLEN 100
#define TOTMEM 5000
char *lptr[MAXLINES];
int my_getline2(char *s, int lim)
{
int c, offset = 0;
while (--lim > 0 && (c = getchar()) != EOF && c != '\n') {
*(s + offset++) = c;
}
if (c == '\n')
*(s + offset++) = c;
s[offset] = '\0';
return offset;
}
int readlines(char *linearr, char *lineptr[], int maxlines)
{
int len, nlines, tot_len;
char line[MAXLEN];
char *p;
nlines = tot_len = 0;
p = linearr;
while ((len = my_getline2(line, MAXLEN)) > 0) {
if ((nlines >= maxlines) || (tot_len + len >= TOTMEM))
return -1;
else {
line[len-1] = '\0';
strcpy(p, line);
lineptr[nlines++] = p;
p += len;
tot_len += len;
}
}
return nlines;
}
void writelines(char *lineptr[], int nlines)
{
while (nlines-- > 0)
printf("%s\n", *lineptr++);
}
int main()
{
char linearr[TOTMEM];
int nol;
nol = readlines(linearr, lptr, MAXLINES);
if (nol != -1)
writelines(lptr, nol);
return 0;
}