school

thing1's amazing school repo
Log | Files | Refs | Submodules | README

fileread.c (869B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <unistd.h>
      5 
      6 #include "util.h"
      7 
      8 typedef struct strings {
      9 	char **strs;
     10 	int count;
     11 } strings;
     12 
     13 //# counts the number of times c ocurrs in s
     14 static int countChars(char *s, char c){ 
     15 	int count = 0;
     16 	for (int i = 0; i < strlen(s); i++){
     17 		if (s[i] == c) count++;	
     18 	}
     19 	return count;
     20 }
     21 
     22 //# returns an array of strings (type strings) of the file contents, split by line
     23 strings *fileread(FILE *f){
     24 	strings *strs = malloc(sizeof(strings));
     25 	strs->strs = malloc(sizeof(char **));
     26 
     27 	char *line = alloca(256);
     28 	int count = 0;
     29 	while (fgets(line, 256, f) != NULL){
     30 		if (line[0] != '\n' && line[0] != '/'){ 
     31 			while (line[0] == '\t') line++;
     32 			line[strlen(line)-1] = '\0';
     33 			strs->strs[count] = malloc(256);
     34 			memcpy(strs->strs[count], line, 256);
     35 			count++;
     36 		}
     37 	}
     38 	strs->count = count;
     39 
     40 	return strs;
     41 }