Write pointer to file in C

后端 未结 7 1566
不思量自难忘°
不思量自难忘° 2020-12-19 09:32

I have a structure:

typedef struct student {
  char *name;
  char *surname;
  int age;
} Student;

I need to write the structure into a bina

相关标签:
7条回答
  • 2020-12-19 09:48

    You need to dereference s and individually write out each element of the structure to actually write thecontents to the file:

    size_t nameCount = strlen(s->name) + 1;
    fwrite(s->name, sizeof(char), nameCount, fp);
    
    size_t surnameCount = strlen(s->surname) + 1;
    fwrite(s->surname, sizeof(char), surnameCount, fp);
    
    0 讨论(0)
  • 2020-12-19 09:50

    if you want to directly write the structure as it is to a file you would want to define the sizes of name and surname rather than dynamically allocating it.

    typedef structure student { char name[100]; char surname[100]; int age; } 
    

    else you will need to write each info in the structure one by one.

    0 讨论(0)
  • 2020-12-19 09:51

    In C, you have to serialize structures (convert them to a series of bytes) manually. If you want the data you output to be readable by another machine, you have to take endianness account endianness.

    Here's a simple example of serializing your structure (writing it to a file), disregarding endianness/differing word size and making the code unportable:

    size_t length;
    
    length = strlen(s->name) + 1;
    fwrite(&length, sizeof(length), 1, fp);
    fwrite(s->name, 1, length, fp);
    
    length = strlen(s->surname) + 1;
    fwrite(&length, sizeof(length), 1, fp);
    fwrite(s->surname, 1, length, fp);
    
    fwrite(&s->age, sizeof(s->age), 1, fp);
    

    And to unserialize:

    size_t length;
    
    fread(&length, sizeof(length), 1, fp);
    s->name = malloc(length);
    fread(s->name, 1, length, fp);
    
    fread(&length, sizeof(length), 1, fp);
    s->surname = malloc(length);
    fread(s->surname, 1, length, fp);
    
    fread(&s->age, sizeof(s->age), 1, fp);
    

    In a real application, you should check the validity of your input rather than blindly assuming the input is valid and trustworthy. Also, you need to decide what byte order you'll use to store your ints, size_ts, etc, and make sure you read/write them in the correct byte order by using bit shifting.

    By the way, you may want to look at tpl, a simple binary serialization library for C.

    0 讨论(0)
  • You need to dereference your pointer and write the parts of the struct. (You should not fwrite a struct directly, but rather encode its parts and write those:

    Student *s = malloc(sizeof(*s));
    s->name = "Jon";
    s->surname = "Skeet";
    s->age = 34;
    
    // ....
    
    fwrite(s->name, sizeof(char), strlen(s->name) + 1, fp);
    fwrite(s->surname, sizeof(char), strlen(s->surname) + 1, fp);
    
    //This one is a bit dangerous, but you get the idea.
    fwrite(&(s->age), sizeof(s->age), 1, fp); 
    
    0 讨论(0)
  • 2020-12-19 10:07

    Note that anyway you won't be able to see the fields in a 'normal' form because you write it to a binary file. Try to open the file without the "b" in the mode argument of fopen (r+\ w\ a etc. instead of rb+\ wb\ ab).

    You can see that when using fread (from the binary file, after opening it in "rb" mode you should get the structure fields as expected.

    0 讨论(0)
  • 2020-12-19 10:11

    You will have to do some extra work to write out the data pointed to by the structure elements - probably just write it out element by element.

    Alternatively change your structure to something like this:

    typedef struct
    {
        char name[MAX_NAME_LEN];
        char surname[MAX_SURNAME_LEN];
        int age;
    } Student;
    
    0 讨论(0)
提交回复
热议问题