-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext2mtf.c
executable file
·112 lines (86 loc) · 2.58 KB
/
text2mtf.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
C is pain. I am become pain
*/
#define BUFFER_LENGTH 100
#define NUM_WORDS 120
#define WORD_LENGTH 21
char wordlist[NUM_WORDS][20];
int wordcount = 0;
int location = 0;
int search( char * word);
void fix_wordlist(void);
int main(int argc, char *argv[]){
/*
PART ONE: FILE I/O
*/
FILE *infile; // pointer to file being opened
FILE *outfile; // pointer to file being written to
char filename[20]; //character array for writing the filename to create the output file name
if(argc < 2){
fprintf(stderr,"Error: No filename specified. Please enter a filename.\n");
exit(1);
}
//this block is used to copy the filename
infile = fopen(argv[1], "r");
strncpy(filename, argv[1], 20 );
// my issue for a while was writing over the null pointer, classic
char * fptr;
fptr = strstr(filename, ".txt");
strncpy(fptr, ".mtf", 5);
outfile = fopen(filename, "w");
//writing magic number
fputc(186, outfile);
fputc(94, outfile);
fputc(186, outfile);
fputc( 17, outfile);
/*
PART TWO: OBTAIN CHUNKS OF THE PROGRAM FOR PARSING
*/
char buffer[BUFFER_LENGTH];
char *buf_ptr;
while(fgets(buffer, sizeof(char) * BUFFER_LENGTH, infile) ) { //while !EOF
buf_ptr = strtok(buffer," ."); //tokenize string by delims
while(buf_ptr != NULL){ // while !end of buffer
if( search(buf_ptr) == 0){
//what do we do here
wordlist[wordcount] = buf_ptr;
wordcount++;
fprintf(outfile, "%c", (128+wordcount) );
fputs(buf_ptr, outfile);
}
// we found a match
else{
fprintf(outfile, "%c", (128+location) );
}
buf_ptr = strtok(NULL, " ."); //places a null terminator and tokenizes from that location
}
fclose(infile);
fclose(outfile);
return 0;
}
}
// searches wordlist for current word to avoid duplicates
int search(char* word){
for(int i = 0; i < wordcount; i ++){
if(strncmp(word, *(wordlist + i), 20) == 0){
//
location = wordcount - i;
fix_wordlist();
return 1;
}
}
return 0;
}
void fix_wordlist(){
char * temp = wordlist[wordcount-location];
for(int i = (wordcount - location); i < (wordcount-1);i++){
wordlist[i] = wordlist[i+1];
}
wordlist[wordcount-1] = temp;
}
/*
two dimensional array time
*/