From 2e1ccff01cf89539b621ac786898229307847f4b Mon Sep 17 00:00:00 2001 From: thing 1 Date: Thu, 14 Nov 2024 08:11:18 +0000 Subject: made too many changes to note --- comp/lucas-standen-NEA/code2/fileread.c | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 comp/lucas-standen-NEA/code2/fileread.c (limited to 'comp/lucas-standen-NEA/code2/fileread.c') diff --git a/comp/lucas-standen-NEA/code2/fileread.c b/comp/lucas-standen-NEA/code2/fileread.c new file mode 100644 index 0000000..0bfc497 --- /dev/null +++ b/comp/lucas-standen-NEA/code2/fileread.c @@ -0,0 +1,41 @@ +#include +#include +#include +#include + +#include "util.h" + +typedef struct strings { + char **strs; + int count; +} strings; + +//# counts the number of times c ocurrs in s +int countChars(char *s, char c){ + int count = 0; + for (int i = 0; i < strlen(s); i++){ + if (s[i] == c) count++; + } + return count; +} + +//# returns an array of strings (type strings) of the file contents, split by line +strings *fileread(FILE *f){ + strings *strs = malloc(sizeof(strings)); + strs->strs = malloc(sizeof(char **)); + + char *line = alloca(256); + int count = 0; + while (fgets(line, 256, f) != NULL){ + if (line[0] != '\n' && line[0] != '/'){ + while (line[0] == '\t') line++; + line[strlen(line)-1] = '\0'; + strs->strs[count] = malloc(256); + memcpy(strs->strs[count], line, 256); + count++; + } + } + strs->count = count; + + return strs; +} -- cgit v1.2.3