-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prices.c
76 lines (62 loc) · 1.58 KB
/
Prices.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
76
/*****************************************************************//**
* \file Prices.c
* \brief
*
* \author Diogo Pinto & Ricardo Cruz
* \date May 2023
*********************************************************************/
#pragma warning(disable:4996)
#pragma region INCLUDES
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "Prices.h"
#define MAX_LINE_SIZE 100
#pragma endregion
#pragma region READ_PRICES_FILE
/**
* \brief Reads prices from text file
*
* \return
*/
Prices* read_prices_from_file()
{
Prices* prices_head = NULL;
FILE* file = fopen("prices.txt", "r");
if (file == NULL)
{
printf("Error opening file.\n");
return prices_head;
}
char line[MAX_LINE_SIZE];
while (fgets(line, MAX_LINE_SIZE, file) != NULL)
{
char* token = strtok(line, ",");
Prices* new_price = (Prices*)malloc(sizeof(Prices));
new_price->type = atoi(token);
token = strtok(NULL, ",");
new_price->price = atof(token);
token = strtok(NULL, ",");
new_price->date = *token;
new_price->next = NULL;
new_price->prev = NULL;
if (prices_head == NULL)
{
prices_head = new_price;
}
else
{
Prices* current = prices_head;
while (current->next != NULL)
{
current = current->next;
}
current->next = new_price;
new_price->prev = current;
}
}
fclose(file);
return prices_head;
}
#pragma endregion