Click here to Skip to main content
16,015,827 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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:
C++
#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.
Posted
Updated 17-Jan-20 14:49pm
Comments
Richard MacCutchan 18-Jan-20 4:57am    
"I've tried to do this for different values in for loop.
Also i know that count is equal to 35."

What does that mean, and what does it have to do with your problem?

We have no idea what you expect that to do: even with the data, reporting it as "it don't work" doesn't help anybody - we can't see your screen, can't access your HDD, or read your mind. And we don't know what your tutor asked you to do!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
Member 14719581 17-Jan-20 17:54pm    
I've found that the problem occurs at the line where i call PrintLog function.
I found that it doesnt allocates memory properly using realloc function.
It returns 3 different addresses "count" times
k5054 17-Jan-20 21:02pm    
You're using realloc incorrectly. If the new size parameter to realloc is larger than the current size of the pointer passed in, then one of three things happens:
1) if there's space available, then the size of the current item is increased. The current value of the pointer will be returned
2) if the new size is larger would overflow to another allocated item, but there is space available for the new item, then new space is allocated, the contents of the current pointer are copied to the new space, and the old space is freed. The value of the new memory area is returned
3) if there is no more memory available, NULL is returned, the contents of the current pointer are not changed or freed

What this means is that code like
ptr = realloc(ptr, new_size);
may fail in unexpected ways. In particular, if realloc is unable to find new space, then ptr will now be NULL, which probably means that you've lost the pointer you did have, and you're leaking memory.What you should do is this
void *tmp_ptr = realloc(ptr, size);
if(tmp_ptr == NULL) {
    // handle error somehow 
}
else
    ptr = tmp_ptr;
// continue processing
Quote:
I cant figure out my mistaked in C code

You forgot to tell what is supposed to do your code ! We can't guess !
You forgot to tell us how the code go wrong (how you know it is wrong).

The only sensible advice is to learn how to use the debugger.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900