-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseArray.c
More file actions
61 lines (51 loc) · 987 Bytes
/
reverseArray.c
File metadata and controls
61 lines (51 loc) · 987 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkedList.h"
#define INITSIZE 64
#define MAXLINE 1024
void usage()
{
fprintf(stderr, "reverse <filename> \n");
}
int main (int argc, char *argv[])
{
unsigned int ui;
char * fname;
FILE * rfile;
char * str;
char linebuf[MAXLINE];
char **lines;
int nlines = 0;
int capacity = INITSIZE;
if (argc != 2 )
{
usage();
exit(-1);
}
fname = argv[1];
if (! (rfile = fopen(fname,"r")))
{
fprintf(stderr,"Could not open file: %s \n", fname);
exit(-1);
}
lines = malloc(capacity * sizeof (char *));
while (fgets(linebuf,MAXLINE-1,rfile) != NULL)
{
linebuf[MAXLINE-1] = '\0'; // force string termination
str = malloc(strlen(linebuf)+1);
strcpy(str,linebuf);
if (nlines > capacity)
{
capacity *= 2;
lines=realloc(lines,capacity * sizeof (char *));
}
lines[nlines++] = str;
}
int i;
for(i = --nlines; i >= 0; i--)
{
printf("%s",lines[i]);
free(lines[i]);
}
}