I cannot wrap my mind around that problem. Program is working properly: Creates 4 txt files and fprintf sorted data there, but when I try to execute second free loop for second linked list it visual says:Debug Assertion Failed: _CrtIsValidHeapPointer(block). Could you help me? Thanks in advance
It almost work properly but this exception is frustrating. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning( disable : 6031 )
#pragma warning( disable : 4996 )
typedef struct pomiar {
unsigned int nr_pomiaru;
unsigned int nr_czujnika;
char data_i_czas[20];
double temp;
struct pomiar* nast;
struct pomiar* poprz;
}pomiar;
int main()
{
printf("Nazwa pliku z danymi: ");
char wejscie[40];
scanf("%s", wejscie);
FILE* input = fopen(wejscie, "r");
if (input == NULL)
{
printf("Nie udalo sie otworzyc pliku z danymi\n");
return 1;
}
pomiar* wsk = NULL, *glowa = NULL;
while (!feof(input))
{
if (glowa == NULL)
{
glowa = wsk = malloc(sizeof(pomiar));
if (wsk == NULL)
{
printf("Blad alokacji pamieci\n");
return 1;
}
wsk->poprz = NULL;
}
else
{
wsk->nast = malloc(sizeof(pomiar));
if (wsk->nast == NULL)
{
printf("Blad alokacji pamieci\n");
return 1;
}
wsk->nast->poprz = wsk;
wsk = wsk->nast;
}
fscanf(input, "%d %d %s %lf", &wsk->nr_pomiaru, &wsk->nr_czujnika, wsk->data_i_czas, &wsk->temp);
wsk->nast = NULL;
}
fclose(input);
// dividing on for lists
pomiar* glowa1 = NULL, * glowa2 = NULL, * glowa3 = NULL, * glowa4 = NULL;
pomiar* wsk1 = NULL, * wsk2 = NULL, * wsk3 = NULL, * wsk4 = NULL;
wsk = glowa;
while (wsk != NULL)
{
if (wsk->nr_czujnika == 1)
{
if (glowa1 == NULL)
{
glowa1 = wsk1 = wsk;
wsk->poprz = NULL;
}
else
{
wsk1->nast = wsk;
wsk->poprz = wsk1;
wsk1 = wsk;
}
}
else if (wsk->nr_czujnika == 2)
{
if (glowa2 == NULL)
{
glowa2 = wsk2 = wsk;
wsk->poprz = NULL;
}
else
{
wsk2->nast = wsk;
wsk->poprz = wsk2;
wsk2 = wsk;
}
}
else if (wsk->nr_czujnika == 3)
{
if (glowa3 == NULL)
{
glowa3 = wsk3 = wsk;
wsk->poprz = NULL;
}
else
{
wsk3->nast = wsk;
wsk->poprz = wsk3;
wsk3 = wsk;
}
}
else if (wsk->nr_czujnika == 4)
{
if (glowa4 == NULL)
{
glowa4 = wsk4 = wsk;
wsk->poprz = NULL;
}
else
{
wsk4->nast = wsk;
wsk->poprz = wsk4;
wsk4 = wsk;
}
}
wsk = wsk->nast;
}
//highest and lowest temps and count of data for each sensor
int ilosc1 = 0, ilosc2 = 0, ilosc3 = 0, ilosc4 = 0;
double min1 = 1000, max1 = 0;
double min2 = 1000, max2 = 0;
double min3 = 1000, max3 = 0;
double min4 = 1000, max4 = 0;
pomiar* tmp = glowa1;
while (tmp != NULL)
{
ilosc1++;
if (tmp->temp < min1) min1 = tmp->temp;
if (tmp->temp > max1) max1 = tmp->temp;
tmp = tmp->nast;
}
tmp = glowa2;
while (tmp != NULL)
{
ilosc2++;
if (tmp->temp < min2) min2 = tmp->temp;
if (tmp->temp > max2) max2 = tmp->temp;
tmp = tmp->nast;
}
tmp = glowa3;
while (tmp != NULL)
{
ilosc3++;
if (tmp->temp < min3) min3 = tmp->temp;
if (tmp->temp > max3) max3 = tmp->temp;
tmp = tmp->nast;
}
tmp = glowa4;
while (tmp != NULL)
{
ilosc4++;
if (tmp->temp < min4) min4 = tmp->temp;
if (tmp->temp > max4) max4 = tmp->temp;
tmp = tmp->nast;
}
printf("Z pliku \"%s\" odczytano:\n", wejscie);
printf("- z czujnika 1: %d pomiarow, temp. min = %lf, temp. max = %lf\n", ilosc1, min1, max1);
printf("- z czujnika 2: %d pomiarow, temp. min = %lf, temp. max = %lf\n", ilosc2, min2, max2);
printf("- z czujnika 3: %d pomiarow, temp. min = %lf, temp. max = %lf\n", ilosc3, min3, max3);
printf("- z czujnika 4: %d pomiarow, temp. min = %lf, temp. max = %lf\n", ilosc4, min4, max4);
//Saving to 4 files
char wyjscie[40];
printf("Podaj poczatek nazwy plikow do zapisu: ");
scanf("%s", wyjscie);
char wyjscie1[40];
char wyjscie2[40];
char wyjscie3[40];
char wyjscie4[40];
sprintf(wyjscie1, "%s%d.txt", wyjscie, 1);
FILE* output1 = fopen(wyjscie1, "w"); // otwarcie pliku do zapisu
if (output1 == NULL)
{
printf("Nie udało się otworzyc pliku do zapisu\n");
return 1;
}
tmp = glowa1;
while (tmp != NULL)
{
fprintf(output1, "%i %i %s %lf\n", tmp->nr_pomiaru, tmp->nr_czujnika, tmp->data_i_czas, tmp->temp);
tmp = tmp->nast;
}
fclose(output1);
sprintf(wyjscie2, "%s%d.txt", wyjscie, 2);
FILE* output2 = fopen(wyjscie2, "w"); // otwarcie pliku do zapisu
if (output2 == NULL)
{
printf("Nie udało się otworzyć pliku do zapisu\n");
return 1;
}
tmp = glowa2;
while (tmp != NULL)
{
fprintf(output2, "%i %i %s %lf\n", tmp->nr_pomiaru, tmp->nr_czujnika, tmp->data_i_czas, tmp->temp);
tmp = tmp->nast;
}
fclose(output2);
sprintf(wyjscie3, "%s%d.txt", wyjscie, 3);
FILE* output3 = fopen(wyjscie3, "w"); // otwarcie pliku do zapisu
if (output3 == NULL)
{
printf("Nie udało się otworzyć pliku do zapisu\n");
return 1;
}
tmp = glowa3;
while (tmp != NULL)
{
fprintf(output3, "%i %i %s %lf\n", tmp->nr_pomiaru, tmp->nr_czujnika, tmp->data_i_czas, tmp->temp);
tmp = tmp->nast;
}
fclose(output3);
sprintf(wyjscie4, "%s%d.txt", wyjscie, 4);
FILE* output4 = fopen(wyjscie4, "w"); // otwarcie pliku do zapisu
if (output4 == NULL)
{
printf("Nie udało się otworzyć pliku do zapisu\n");
return 1;
}
tmp = glowa4;
while (tmp != NULL)
{
fprintf(output4, "%i %i %s %lf\n", tmp->nr_pomiaru, tmp->nr_czujnika, tmp->data_i_czas, tmp->temp);
tmp = tmp->nast;
}
fclose(output4);
// Freeing memory!!!
tmp = glowa1;
while (tmp != NULL)
{
glowa1 = glowa1->nast;
free(tmp);
tmp = glowa1;
}
tmp = glowa2;
while (tmp != NULL)
{
glowa2 = glowa2->nast;
free(tmp);
tmp = glowa2;
}
tmp = glowa3;
while (tmp != NULL)
{
glowa3 = glowa3->nast;
free(tmp);
tmp = glowa3;
}
tmp = glowa4;
while (tmp != NULL)
{
glowa4 = glowa4->nast;
free(tmp);
tmp = glowa4;
}
return 0;
}