-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_sample.c
76 lines (66 loc) · 1.65 KB
/
csv_sample.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getWordCount(const char* text, int length)
{
int wordCount = 0;
if(length >= 2) //Due to quotes;
{
wordCount += 1;
}
int i = 0;
for(i = 0; i < length; i++){
if(' ' == text[i])
{
wordCount++;
}
}
return wordCount;
}
const char* getfield(char* line, int num)
{
const char* tok;
//for (tok = strtok(line, ","); tok && *tok; tok = strtok(NULL, ",\n"))
for (tok = strtok(line, ","); ; tok = strtok(NULL, ",\n"))
{
if (!--num)
return tok;
}
return NULL;
}
int main(int argc, char** argv)
{
printf("Input file: %s\n", argv[1]);
FILE* stream = fopen(argv[1], "r");
char line[1024];
float lines = 0;
float lenTotal = 0;
while (fgets(line, 1024, stream))
{
char* tmp = strdup(line);
const char* out = getfield(tmp, 12);
float nextLength = strlen(out);
lines++;
printf("Text: %s\n", out);
printf("Length: %f\n", nextLength);
float wC = getWordCount(out, nextLength);
printf("Word Count: %f\n", wC);
int aveCPW = (nextLength - (wC-1))/wC;
printf("Chars per word: %d\n", aveCPW);
char* wordTwoGuess = (char *) malloc(aveCPW+1 * sizeof(char));
memcpy(wordTwoGuess, &out[aveCPW + 1], aveCPW);
printf("Guess at second word: %s\n", wordTwoGuess);
if(wC == 2)
{
printf("%c\n", out[2000048]);
}
if(wC > 5)
{
free(tmp);
}
lenTotal += nextLength;
// NOTE strtok clobbers tmp
free(tmp);
}
printf("Average Tweet Length: %f\n", lenTotal/lines);
}