-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileSplitter.c
84 lines (65 loc) · 1.76 KB
/
fileSplitter.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <string.h>
const int _ORIGIN_ = 0;
const char _DELIM_ = ' ';
const char _NEW_LINE_ = '\n';
const char _TAB_ = '\t';
int main( int argc, char *argv[] ) {
char firstDelimFlag = 'F', lastDelimFlag = 'T', gramFile[100], tempChar;
FILE *ipFile, *opFile;
int delimCnt = 0, firstDelimPos, charRead;
if( argc < 3 ) {
printf( "Invalid no of arguments\n" );
printf( "Usage: ./fileSplitter <file to be splitted> <no of grams required>\n" );
return 1;
}
ipFile = fopen( argv[1], "r" );
if ( NULL == ipFile ) {
perror( "Error opening file" );
return 1;
}
strcat( gramFile, argv[1] );
strcat( gramFile, "." );
strcat( gramFile, argv[2] );
opFile = fopen( gramFile, "w" );
if ( NULL == ipFile ) {
perror( "Error opening file" );
return 1;
}
const int gramLimit = atoi( argv[2] );
while ( !feof( ipFile ) ){
charRead = fgetc( ipFile );
if( EOF == charRead )
break;
if ( ( _DELIM_ == charRead ) || ( _NEW_LINE_ == charRead ) || ( _TAB_ == charRead ) ) {
tempChar = fgetc( ipFile );
while ( ( tempChar == _NEW_LINE_ ) || ( tempChar == _DELIM_ ) || ( tempChar == _TAB_ ) ) {
tempChar = fgetc( ipFile );
if( EOF == tempChar )
break;
}
if( EOF == tempChar )
break;
fseek ( ipFile, ftell ( ipFile ) - 1, _ORIGIN_ );
if( ( _NEW_LINE_ == charRead ) || ( _TAB_ == charRead ) )
charRead = _DELIM_;
if( _DELIM_ == charRead ) {
if ( 'F' == firstDelimFlag ){
firstDelimPos = ftell( ipFile );
firstDelimFlag = 'T';
}
++delimCnt;
if ( delimCnt == gramLimit ){
fseek( ipFile, firstDelimPos, _ORIGIN_ );
firstDelimFlag = 'F';
charRead = _NEW_LINE_;
delimCnt = 0;
}
}
}
fputc( charRead, opFile );
}
fclose(ipFile);
fclose(opFile);
return 0;
}