-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse.c
53 lines (41 loc) · 1.82 KB
/
reverse.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
//Initializing two file pointers.
FILE* fp = fopen(argv[1], "r");
FILE* fpOut = fopen(argv[2], "w");
const int bufferSize = 3000; //Global buffer.
const int lineLength = 200; //Line buffer.
int bytesRead = 0, len, i = 0;
char bufferArr[lineLength];
int linePtr[bufferSize];
//Error handling
if (fp == NULL || fpOut == NULL) {
printf("Error reading/writing file.\n");
exit(1);
}
linePtr[i] = 0; //Initializing 0th index to zero.
//Reading the input file line-by-line.
while (fgets(bufferArr, sizeof(bufferArr), fp)) {
len = strlen(bufferArr); //Storing the length of the line read in len variable.
bytesRead += len; //Accounting for total no. of bytes read.
/* Incrementing i for indexing in the linePtr array, as we're going to store
the nth byte in linePtr array, pointing to the start of each line in the
input file. */
i++;
linePtr[i] = bytesRead; //Storing the specific no. of bytes (index) at which each new line starts.
}
rewind(fp); //Seeking back to file start.
--i; //Decrementing i to account for newline character.
//Again, reading input line-by-line and writing to output file, in reverse.
while (i >= 0) {
fseek(fp, linePtr[i], 0); //Seeking to nth byte where new line each line starts.
fgets(bufferArr, sizeof(bufferArr), fp);
fprintf(fpOut, "%s", bufferArr);
i--; //Decrementing i to output lines in reverse.
}
fclose(fp);
fclose(fpOut);
exit(0);
}