Vlad
Vlad

Reputation: 91

Save a struct into a file then read it in the same program

I have this struct which I stack with information, I'm doing that via pointers, after doing that I'm saving all the info into a file named person.txt and then read it in the same program. The problems I'm having are:

  1. I can display the final result correctly, but, the person.txt file doesn't have any meaningful text in it, it's just a bunch of unknown symbols for me (I guess it's automatically saved in bit, but I don't know why it does that).
  2. After inserting the info and the .txt file is created and the result is displayed, after closing the program, when trying to use only the code to read the file (meaning I make a commentary out of the code I won't need), it displays me something else, totally different of what I initially introduced.

Heres the code:

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

struct eu
{
    char name[30];
    int age,weight, number;
};

int main()
{
    struct eu *Eu1, euptr;
    Eu1 = &euptr;

    printf("Name & Surname:");
    scanf("%[^\n]", Eu1->name);
    printf("\n");
    printf("Age:");
    scanf("%d", &Eu1->age);
    printf("\n");
    printf("Weigt(kg):");
    scanf("%d", &Eu1->weight);
    printf("\n");
    printf("Telephone number:");
    scanf("%d", &Eu1->number);
    printf("\n\nDisplay: ");

    FILE *outinfo;
    outinfo = fopen("person.txt", "a");

    if (outinfo == NULL)
    {
        fprintf(stderr, "\nSomething went wrong!\n");
    }

    fwrite(&Eu1, sizeof(struct eu),1,outinfo);

    fclose(outinfo);

    outinfo = fopen("person.txt", "r");

    while(fread(&Eu1, sizeof(struct eu), 1, outinfo))
        printf ("\n Name:%s\n Age:%d\n Weight:%d\n Tel. Number:%d\n ", Eu1->name, Eu1->age, Eu1->weight, Eu1->number );

    fclose (outinfo);


    return 0;
}

Upvotes: 0

Views: 64

Answers (2)

leo
leo

Reputation: 522

you've messed the pointers. It is ok to save whole struct into the file, here is correctly working code:

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

struct eu
{
    char name[30];
    int age,weight, number;
};

int main()
{
    struct eu eu1, *euptr;
    euptr = &eu1;


    printf("Name & Surname:");
    scanf("%[^\n]", euptr->name);
    printf("\n");
    printf("Age:");
    scanf("%d", &euptr->age);
    printf("\n");
    printf("Weigt(kg):");
    scanf("%d", &euptr->weight);
    printf("\n");
    printf("Telephone number:");
    scanf("%d", &euptr->number);
    printf("\n\nDisplay: ");

    FILE *outinfo;
    outinfo = fopen("person.txt", "a");

    if (outinfo == NULL)
    {
        fprintf(stderr, "\nSomething went wrong!\n");
    }

    fwrite(euptr, sizeof(struct eu), 1, outinfo);
    fclose(outinfo);

    outinfo = fopen("person.txt", "r");
    while(fread(&eu1, sizeof(struct eu), 1, outinfo))
        printf ("\n Name:%s\n Age:%d\n Weight:%d\n Tel. Number:%d\n ", euptr->name, euptr->age, euptr->weight, euptr->number );

    fclose (outinfo);
    return 0;
}

I've swapped eu1 & euptr variables for more logical naming.

Upvotes: 1

rtpax
rtpax

Reputation: 1777

fwrite will write the raw data you have given it, for the number of bytes you have specified. You are able to write this struct and read it back which is exactly what you are trying to do, all is working as intended. "Normal" text files with words use a particular encoding (usually ascii or unicode), and you need to use that encoding if you want it to be readable.

If you wanted this to be human readable, you would need to specify a format. A very simple one would be CSV (comma separated values).

So you might do something like

fprintf(outinfo, "%s,%d,%d,%d", Eu1.name, Eu1.age, Eu1.weight, Eu1.number);
//...
fseek(outinfo,0,SEEK_SET);// move to beginning of file
fscanf(outinfo, "%s,%d,%d,%d", &Eu2.name, &Eu2.age, &Eu2.weight, &Eu2.number);

this doesn't involve any error checking, and would need to be changed if your struct format changed, but would allow the file to be human readable if that was a requirement for you.

Upvotes: 0

Related Questions