-
Notifications
You must be signed in to change notification settings - Fork 0
/
inverseLines.c
59 lines (59 loc) · 1.39 KB
/
inverseLines.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
/*
Inverse the lines of a text file
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE 256
int main(int argc,char *argv[])
{
FILE *in,*out;
char *s=(char*)calloc(LINE,sizeof(char));
int lineCount=0,index=0,i;
fpos_t position,*positions;
if(argc!=2)
{
printf("inverseLines <input-file>\r\n");
return -1;
}
if(NULL==(in=fopen(argv[1],"r")))
{
printf("Error opening file:%s\r\n",argv[1]);
return -2;
}
if(NULL==(out=fopen(strcat(argv[1],".inversed"),"w")))
{
printf("Error creating output file:%s\r\n",strcat(argv[1],".inversed"));
return -3;
}
//start @ the end of inputfile and write lines to the outputfile
while(NULL!=fgets(s,LINE,in))//determine line count
{
lineCount++;
}
positions=(fpos_t *)calloc(lineCount,sizeof(fpos_t));//for storing line positions
fseek(in,0,SEEK_SET);//reset file pointer
// positions[index++]=(fpos_t)0;//first line starts @ offset 0
while(NULL!=fgets(s,LINE,in))
{
if(-1==fgetpos(in,&positions[index++]))
{
printf("Error fgetpos\r\n");
return -4;
}
}
for(i=lineCount-2;i>=0;i--)
{
fsetpos(in,&positions[i]);
printf("%s",fgets(s,LINE,in));
fputs(s,out);
}
fseek(in,0,SEEK_SET);//get the first line
printf("%s",fgets(s,LINE,in));
fputs(s,out);
fclose(out);
fclose(in);
free(positions);
free(s);
return 0;
}