0

Tried to debug and figure out to find a solution but i all the time get the same memory error I have a file

2015-07-22 09:00:00 1346137 13.03 25.13 6.474 3.805 0.832 25.84
2015-07-22 09:01:00 1346138 13.03 25.15 6.5 3.84 0.834 25.89

and i try to pass these values to a struct and the write them in binary to another file. later i must take the binary file read it and paste its contents to console in this view:

| Time Stamp  | Temperature   |     Measurements   |

|Hour min sec  |Environmental |  m1      m2     m3   |

==================================

|   9 : 0 : 0     |        25.84        | 6.47 | 3.81 | 0.83 |

|   9 : 1 : 0     |        25.89        | 6.50 | 3.84 | 0.83 |

|   9 : 2 : 0     |        26.02        | 6.48 | 3.85 | 0.84 |

below is my source code that i've rewrite again with different way

    #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);

    for (int i = 0; i < 2; i++)
    {
        PrintLog(logs[i]);
    }

    return EXIT_SUCCESS;
}

what im doing wrong and i get error?

  • Just some random drive-by observations, and maybe I'm just dumb, but wouldn't the fscanf(fptr, "%s", log->date); bit in createLlog() grab the whole line? I would make sure that's only grabbing until the space like intended. Also does the fgetc(fptr) in main() move the file cursor? Are you still pointed at the first character when you scan in the date? My C is a little rusty, so I can't remember. – TopherIsSwell Jan 17 '20 at 18:30
  • Perhaps more importantly, what error are you getting? Is it a runtime error? – TopherIsSwell Jan 17 '20 at 18:32
  • Please make sure you are compiling with a C compiler. `(Time*)` is invalid, Time is a struct, it needs `struct Time`. As all the other `sizeof(MyLog)` etc. `i get error?` What error do you get? Please post the exact error message. You are leaking a lot of memory each loop. One of the reasons __not to__ use typedef pointers. `while (ch != EOF)` this is endless loop. You have no error checking in your code. You should check retrurn value of each `fopen`, each `malloc`, each `scanf`. `"fscanf(.., %s", log->date` can overflow `log->date` - how long is date? – KamilCuk Jan 17 '20 at 18:54
  • My compiler returns 20 errors, 5 warnings. The biggest hitters for errors on about structs.. _16, 5 error: must use 'struct' tag to refer to type 'Time'_ , _ 98, 43 error: incomplete definition of type 'struct list'_, etc. are you using the error messages that are output from your compiler? They are very useful to point out where, and what the errors are, thus giving hints on how to work though them to get working code. – ryyker Jan 17 '20 at 19:10
  • [Do I cast the result of malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – RobertS supports Monica Cellio Jan 17 '20 at 19:16
  • @TopherIsSwell that does not grab the whole line, but stops at the first whitespace character. – Weather Vane Jan 17 '20 at 19:27
  • `int c`. And you're never reading more characters after the first one – Antti Haapala -- Слава Україні Jan 17 '20 at 19:33
  • it opens an error window a microsoft visual c++ runtime library program:...my program.exe File:...gfetc.cpp line 43 expression:stream.valid() – TomatoPaste Jan 17 '20 at 19:34

0 Answers0