-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileFunctions.c
More file actions
144 lines (128 loc) · 3.41 KB
/
FileFunctions.c
File metadata and controls
144 lines (128 loc) · 3.41 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
** Name: Jack Lardner
** Student ID: 183*****
**
** Contains methods relating to file IO
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "LinkedList.h"
/*
uses int pointer for error codes
0 for success
-1 for file open fail
*/
LinkedList* readFile(char* inFile, int* exitCode)
{
FILE* file = fopen(inFile, "r");
LinkedList* list;
if (file == NULL)
{
perror("Error opening input file");
*exitCode = -1;
}
else
{
char temp[500];
char letter;
char* token;
char delimiters[] = ",\n";
char** array;
int ii, numAttributes = 0;
/* finds how many attributes and stores in numAttributes
then create linked list */
fgets(temp, 500, file);
for (ii = 0; ii < strlen(temp); ii++)
{
letter = temp[ii];
if (letter == ',')
{
numAttributes++;
}
}
numAttributes++;
/* declaring list and allocating temp array */
list = createLinkedList(numAttributes);
array = (char**)malloc((numAttributes) * sizeof(char*));
/* storing entries into linked list */
while (fgets(temp, 500, file) != NULL)
{
token = strtok(temp, delimiters);
ii = 0;
while (token != NULL)
{
array[ii] = malloc(strlen(token) * sizeof(char) + 1);
strcpy(array[ii], token);
token = strtok(NULL, delimiters);
ii++;
}
addNode(list, array);
for (ii = 0; ii < numAttributes; ii++)
{
free(array[ii]);
}
}
#ifdef DEBUG
printf("\n~ Number of attributes: %d\n~ Number of entries: %d\n", numAttributes, numEntries);
printf("\n~ Output of linked list:\n\n");
printList(list);
printf("\n");
#endif
free(array);
*exitCode = 0;
fclose(file);
}
return list;
}
/* Returns int for error code
0 for success
-1 for file write fail
*/
int writeFile(LinkedList* list, char* outFileName, char* inFileName)
{
int retVal = 0;
FILE* outFile = fopen(outFileName, "w");
/* error checking */
if (outFile == NULL)
{
perror("Error writing to output file");
retVal = -1;
}
else
{
FILE* inFile;
char temp[500];
LinkedListNode* current;
int ii;
/*
writes the first line of the input file
to the first line of the output file
*/
inFile = fopen(inFileName, "r");
fgets(temp, 500, inFile);
fclose(inFile);
fputs(temp, outFile);
/* traverse through linked list */
current = list->head;
while (current != NULL)
{
for (ii = 0; ii < list->numAttributes; ii++)
{
fputs(current->data[ii], outFile);
/*
write a comma at the end of
each attribute except for the last one
*/
if (ii != list->numAttributes - 1)
{
fputc(',', outFile);
}
}
fputc('\n', outFile);
current = current->next;
}
fclose(outFile);
}
return retVal;
}