-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.c
86 lines (62 loc) · 1.57 KB
/
read.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
/*
COMP20003 Assignment Two (read.c)
Created by Yun-Chi Hsiao (student ID: 1074004) Sep 15th, 2020
*/
#include "read.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
/******************************************************************************/
char* readDataFileName(char **argv) {
char *dataFileName = argv[1];
if (!dataFileName) {
printf("DataFile Name Error!");
exit(1);
}
return dataFileName;
}
char* readOutputFileName(char **argv) {
char *outputFileName = argv[2];
if (!outputFileName) {
printf("OutputFile Name Error!");
exit(1);
}
return outputFileName;
}
FILE* openDataFile(char *dataFileName) {
FILE *dataFile = fopen(dataFileName, "r");
if (!dataFile) {
printf("OPEN DataFile Error!");
exit(1);
}
return dataFile;
}
FILE* openOutputFile(char *outputFileName) {
FILE *outputFile = fopen(outputFileName, "w");
if (!outputFile) {
printf("OPEN OutputFile Error!");
exit(1);
}
return outputFile;
}
void createField(char *line, char *fields[]) {
int n = 0;
char *token = strtok(line, STRCOMMA);
while (token) {
fields[n] = (char*)malloc(sizeof(char*)*strlen(token));
assert(fields[n]);
strcpy(fields[n], token);
token = strtok(NULL, STRCOMMA);
n++;
}
/* Replaces the last two characters in location ("\r\n") to termination */
fields[NUMBERFIELDS - 1][strlen(fields[NUMBERFIELDS - 1]) - 1] = TERMINATE;
fields[NUMBERFIELDS - 1][strlen(fields[NUMBERFIELDS - 1]) - 2] = TERMINATE;
}
void freeFields(char *fields[]) {
int i = 0;
for (i = 0; i < NUMBERFIELDS; i++) {
free(fields[i]);
}
}