TheProgrammer
TheProgrammer

Reputation: 824

Bytes seem to be shifted

I wrote a program and there is one bug in it. I tried to solve it since yesterday midday but it seems to be impossible. I have removed a lot of methods here to just let you see important code. The bug occurs when I read in the data from the file. The reading works and the writing on the array should work too. But if I output the data (with ausgabeBS()) after reading them there are completely random numbers on it. Please look at case 6 of the switch and in the method read().

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

typedef struct baustein
{
    int hoehe;
    int breite;
    int length;
    int rot;
    int gruen;
    int blau;
} BAUSTEIN;

void eingabeBS(BAUSTEIN *bs);
void ausgabeBS(BAUSTEIN bs);
int compareBS(BAUSTEIN bs1, BAUSTEIN bs2);
void rem(BAUSTEIN *bs, int stelle, int length);
int auswahl();
int getVolume(BAUSTEIN bs);
void compare(int length, BAUSTEIN *lego);
void quicksort(int links, int rechts, BAUSTEIN feld[]);
void speichern(int length, BAUSTEIN *lego);
int read(BAUSTEIN *lego, int length);

int main()
{
    BAUSTEIN *legobausteine;
    legobausteine = (BAUSTEIN*)malloc(1 * sizeof(BAUSTEIN));
    int length = 0;
    int command = 1;
    int chosen;
    while (1)
    {
        system("cls");
        command = auswahl();
        switch (command)
        {
        case 0:
            length++;
            legobausteine = (BAUSTEIN *)realloc(legobausteine, sizeof(BAUSTEIN) * length);
            eingabeBS(&legobausteine[length - 1]);
            break;
        case 1:
            printf("Welches der Elemente von [0; %d[ soll ausgegeben werden: ", length);
            scanf("%d", &chosen);
            if (chosen < 0 || chosen > length - 1)
            {
                printf("Dies ist keine gueltige Eingabe!\n");
                system("pause");
                break;
            }
            ausgabeBS(legobausteine[chosen]);
            break;
        case 2:
            printf("Welches der Elemente von [0; %d[ soll entfernt werden: ", length);
            scanf("%d", &chosen);
            if (chosen < 0 || chosen > length - 1)
            {
                printf("Dies ist keine gueltige Eingabe!\n");
                system("pause");
                break;
            }
            rem(legobausteine, chosen, length);
            length--;
            break;
        case 3:
            compare(length, legobausteine);
            break;
        case 4:
            if (length != 0)
            {
                quicksort(0, length - 1, legobausteine);
            }
            else
            {
                printf("\nEs sind keine Inhalte vorhanden!\n");
                system("pause");
            }
            break;
        case 5:
            speichern(length, legobausteine);
            break;
        case 6:
            length = read(legobausteine, length);
            printf("FINAL: %d", legobausteine[length - 1].length); //here a random number is outputed.
            break;
        case 7:
            return 0;
        default:
            printf("Das ist kein gueltiger Befehl!\n");
            system("pause");
            break;
        }
    }
    return 0;
}

void speichern(int length, BAUSTEIN *lego)
{
    FILE *f = fopen("data.txt", "w");

    for (int i = 0; i < length; i++)
    {
        fprintf(f, "%d;%d;%d;%d;%d;%d;", lego[i].hoehe, lego[i].breite, lego[i].length, lego[i].rot, lego[i].gruen, lego[i].blau);
    }
    fprintf(f, "\n");
    fclose(f);
}

int read(BAUSTEIN *lego, int length)
{
    FILE *f;
    f = fopen("data.txt", "r");
    if (f)
    {
        int c;
        char buff[300];
        int counter = 0;
        printf("HIER \n");
        while ((c = getc(f)) != EOF)
        {
            buff[counter] = c;
            printf("%c", c);
            counter++;
        }
        fclose(f);

        char *data;
        data = strtok(buff, "\n");
        printf("Daten: %    s", &data[0]);
        int wert = 0;
        char *temp;

        temp = strtok(data, ";");

        while(temp != NULL){
            if(wert % 6 == 0){
                printf("\nReallokiert!\n");
                length++;
                lego = (BAUSTEIN *)realloc(lego, sizeof(BAUSTEIN) * length);
            }
            printf("\n%d", atoi(temp));
            switch(wert % 6){
                case 0:
                    lego[length - 1].hoehe = atoi(temp);
                    printf("Wert hier: %d\n",lego[length-1].hoehe);
                case 1:
                    lego[length - 1].breite = atoi(temp);
                case 2:
                    lego[length - 1].length = atoi(temp);
                case 3:
                    lego[length - 1].rot = atoi(temp);
                case 4:
                    lego[length - 1].gruen = atoi(temp);
                case 5:
                    lego[length - 1].blau = atoi(temp);
                }
            temp = strtok(NULL, ";");
            wert++;
        }
    }
    else
    {
        printf("Es sind noch keine Daten vorhanden!");
    }
    return length;
}

void ausgabeBS(BAUSTEIN bs)
{
    printf("Die Hoehe betraegt %d mm\n", bs.hoehe);
    printf("Die Breite betraegt %d mm\n", bs.breite);
    printf("Die length betraegt %d mm\n", bs.length);
    printf("Der Rotwert betraegt %d \n", bs.rot);
    printf("Der Gruenwert betraegt %d \n", bs.gruen);
    printf("Der Blauwert betraegt %d \n", bs.blau);
    system("pause");
}

Upvotes: 0

Views: 71

Answers (1)

user7860670
user7860670

Reputation: 37578

Inside of read you reallocate buffer for structures storing a new value in lego and invalidating previous once, however lego is a function parameter so calling code still has an old (now invalid) pointer legobausteine. If you are going to reallocate it then you need to pass a pointer to this pointer. Also when you initially allocate buffer with one item for legobausteine in main it is never initialized and initially contains garbage.

Upvotes: 4

Related Questions