-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadfile.c
50 lines (41 loc) · 858 Bytes
/
readfile.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "readfile.h"
int static numwords = 0;
// Get a list of strings in the file,
// separated by whitespace
char**
getfilestring (FILE* fp)
{
// words limited to 64 chars
char word[64];
char** list = (char**) malloc(64 * sizeof(char*));
//char** list;
int c = 0;
while (fscanf(fp, "%s", word) != EOF)
{
list[c] = (char*) malloc(sizeof(word));
//fprintf(stdout, "%s\n", word);
strcpy(list[c], word);
c++;
}
numwords = c;
return list;
}
// Should be called after getfilestring,
// or else you'll just get 0
int
getnumwords ()
{
return numwords;
}
// Get the size of the file, in bytes
size_t
getfilesize (FILE* fp)
{
fseek(fp, 0, SEEK_END);
size_t size = ftell(fp);
rewind(fp);
return size;
}