-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.c
57 lines (49 loc) · 1.64 KB
/
reader.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "reader.h"
struct words *read_lexicon(const char *filename, int *num_words) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Error opening lexicon file");
return NULL;
}
// Allocate memory for initial word_sentiments array
int max_words = MAX_WORDS;
struct words *word_sentiments = malloc(max_words * sizeof(struct words));
if (word_sentiments == NULL) {
// Handle memory allocation error
perror("Memory allocation Failed!!");
fclose(file);
return NULL;
}
*num_words = 0;
// Read each line from the lexicon file
char line[MAX_LINE_LENGTH];
while (fgets(line, MAX_LINE_LENGTH, file) != NULL) {
// Parsing line
char word[MAX_WORD_LENGTH];
float score;
if (sscanf(line, "%s %f", word, &score) == 2) {
// Copy values to struct
strcpy(word_sentiments[*num_words].word, word);
word_sentiments[*num_words].score = score;
(*num_words)++;
// Check for memory reallocation
if (*num_words >= max_words) {
max_words *= 2;
struct words *temp = realloc(word_sentiments, max_words * sizeof(struct words));
if (temp == NULL) {
perror("Memory reallocation Failed!!");
fclose(file);
free(word_sentiments);
return NULL;
}
word_sentiments = temp;
}
}
}
// Close the file
fclose(file);
return word_sentiments;
}