Hello guys! whats up?
I'm trying couple of hours to solve an annoying mistake and get the expected result but every try i do fails.
Could you help me please?
This is my source code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define READ_ONLY "r"
#define WRITE_BINARY "wb"
#define READ_BINARY "rb"
#define MEM_ALLOC_ERR 1234
#define NO_SUCH_PATH 1235
#define NEW(type, elements) ((type* )calloc(elements, sizeof(type)))
#define LOG(message, code) {printf("%s", message); exit(code);}
typedef struct Time {
int hh, mm, ss;
}*p_time_t;
typedef struct Log
{
char date[11];
p_time_t time;
uint32_t index;
double voltage,
logger_temperature,
sun_shooting,
sixth_field,
seventh_field,
env_temperature;
}*p_log_t;
FILE* OpenFile(const char* path, const char* type) {
FILE* fptr = NULL;
fptr = fopen(path, type);
if (!fptr)LOG("File cant be found", NO_SUCH_PATH);
return fptr;
}
p_log_t CreateLog()
{
p_log_t new_log = NULL;
new_log = NEW(Log, 1);
if (!new_log)LOG("Memmory allocation failed", MEM_ALLOC_ERR);
new_log->time = NEW(Time, 1);
if (!new_log->time)LOG("Memmory allocation failed", MEM_ALLOC_ERR);
return new_log;
}
void InitLog(FILE* fptr, p_log_t log){
if(fscanf(fptr, "%s %d:%d:%d %u %lf %lf %lf %lf %lf %lf",
log->date,
&(log->time->hh),
&(log->time->mm),
&(log->time->ss),
&log->index,
&log->voltage,
&log->logger_temperature,
&log->sun_shooting,
&log->sixth_field,
&log->seventh_field,
&log->env_temperature));
}
void PrintLog(p_log_t log)
{
printf("%s %d %d %d %u %lf %lf %lf %lf %lf %lf",
log->date,
log->time->hh,
log->time->mm,
log->time->ss,
log->index,
log->voltage,
log->logger_temperature,
log->sun_shooting,
log->sixth_field,
log->seventh_field,
log->env_temperature);
}
int main(int argc, char** argv)
{
FILE* input_file = OpenFile("DataMeteo-E5.txt", READ_ONLY);
p_log_t log = CreateLog();
p_log_t* logs = NULL;
int count = 0;
FILE* output_file = OpenFile("DataMeteo-E5.MyExtension", WRITE_BINARY);
while (!feof(input_file))
{
InitLog(input_file, log);
if (log->env_temperature < 26.5 || log->env_temperature > 32.5)
{
fwrite(log, sizeof(Log), 1, output_file);
count++;
if (count == 1)logs = NEW(p_log_t, 1);
else logs = (p_log_t*)realloc(logs, sizeof(Log)*count);
}
}
fclose(output_file);
output_file = OpenFile("DataMeteo-E5.MyExtension", READ_BINARY);
fread(logs, sizeof(Log), count, output_file);
printf("%d", count);
for (int i = 0; i < count-1; i++)
{
PrintLog(logs[i]);
}
return EXIT_SUCCESS;
}
The contents of my file looks like this:
2015-07-22 09:02:00 1346139 13.03 25.19 6.477 3.851 0.836 26.02
2015-07-22 09:03:00 1346140 13.03 25.22 6.493 3.879 0.841 26.07
2015-07-22 09:04:00 1346141 13.02 25.25 6.516 3.91 0.846 26.01
2015-07-22 09:05:00 1346142 13.03 25.29 6.537 3.94 0.846 25.99
2015-07-22 09:06:00 1346143 13.02 25.32 6.574 3.976 0.843 26.19
2015-07-22 09:07:00 1346144 13.02 25.36 6.606 4.012 0.845 26.3
2015-07-22 09:08:00 1346145 13.02 25.38 6.656 4.057 0.848 26.4
2015-07-22 09:09:00 1346146 13.02 25.43 6.667 4.086 0.849 26.45
2015-07-22 09:10:00 1346147 13.02 25.46 6.663 4.109 0.851 26.44
2015-07-22 09:11:00 1346148 13.02 25.5 6.657 4.131 0.856 26.51
2015-07-22 09:12:00 1346149 13.02 25.53 6.693 4.17 0.862 26.53
2015-07-22 09:13:00 1346150 13.02 25.56 6.723 4.205 0.865 26.71
2015-07-22 09:14:00 1346151 13.02 25.6 6.734 4.233 0.866 26.64
2015-07-22 09:15:00 1346152 13.02 25.63 6.774 4.269 0.863 26.57
2015-07-22 09:16:00 1346153 13.02 25.67 6.768 4.283 0.865 26.63
2015-07-22 09:17:00 1346154 13.02 25.7 6.749 4.293 0.868 26.63
2015-07-22 09:18:00 1346155 13.02 25.73 6.725 4.302 0.87 26.8
2015-07-22 09:19:00 1346156 13.02 25.78 6.728 4.327 0.871 26.85
What I have tried:
I've tried to do this for different values in for loop.
Also i know that count is equal to 35.