-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyPartOfLine.c
46 lines (33 loc) · 929 Bytes
/
copyPartOfLine.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
//Raced someone to do this before they could do pattern matching.
//Same result can be produced with bash with: cat inputFile |colrm 42 > temp && awk '{print $0","}' temp > final
//but this is more c-like and fun
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
FILE *fpIn = NULL;
FILE *fpOut = NULL;
#define CHARS_PER_LINE 27
int main (int argc, char **argv) {
//char lineLength[CHARS_PER_LINE];
fpIn = fopen(argv[1], "rb");
fpOut = fopen(argv[2], "wb");
unsigned char current = 0;
unsigned char comma = ',';
int count = 0;
while(fread(¤t, sizeof(current), 1, fpIn)) {
count++;
if(count <= CHARS_PER_LINE) {
printf("%c", current);
fwrite(¤t, sizeof(current), 1, fpOut);
}
if(current == '\n') {
printf("\n");
fwrite(&comma, sizeof(current), 1, fpOut);
fwrite(¤t, sizeof(current), 1, fpOut);
count = 0;
}
}
fclose(fpIn);
fclose(fpOut);
return 0;
}