-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileOperations.c
More file actions
60 lines (53 loc) · 1.75 KB
/
Copy pathFileOperations.c
File metadata and controls
60 lines (53 loc) · 1.75 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
#include "FileOperations.h"
#include "Helpers.h"
#include <stdlib.h>
#include <string.h>
int ReadFile(FILE *input, const bool verbose, int outputCapacity, char **output)
{
if (verbose)
{
printf("------ READ FILE ------\n");
}
// Get number of bytes in the file
fseek(input, 0L, SEEK_END);
long numBytes = ftell(input);
fseek(input, 0L, SEEK_SET);
// Create one string with all the contents of the file
char *buffer = calloc(numBytes + 1, sizeof(char));
fread(buffer, sizeof(char), numBytes, input);
if (verbose)
{
printf("Input file is %ld bytes\n", numBytes);
printf("Contents of input file:\n%s\n", buffer);
}
// Tokenize by newline character to get an array of lines
const char *delimeter = "\n";
char *line = strtok(buffer, delimeter);
int lineCount = 0;
while (line)
{
// If the line is pure whitespace, skip it
if (!isWhitespace(line))
{
// Allocate space for the line and copy it into the output array
output[lineCount] = calloc(strlen(line) + 1, sizeof(char));
strcpy(output[lineCount], line);
if (verbose)
{
printf("Read line #%d: %s\n", lineCount, output[lineCount]);
}
// Increment the line counter
lineCount++;
// Reallocate the output buffer if it exceeds capacity.
if (lineCount >= outputCapacity)
{
outputCapacity *= 2;
output = realloc(output, outputCapacity);
}
}
// Get the next line
line = strtok(NULL, delimeter);
}
free(buffer);
return lineCount;
}