Reading/Writing structs to files (aka Serialization)

Sdílet
Vložit

Komentáře • 125

  • @kalinmarinov5268
    @kalinmarinov5268 Před 4 lety +33

    Hey, that is the most in-depth explanation for serialization and struct files i have found on CZcams and Stack Overflow. I've been fighting with my code for the past 4 days because i was trying to save a huge dynamic 2d struct to a bin file, but didn't know why the reading from file was failing. I used the ideas about format specifying and coma separation and did the job. Really glad i found your channel. + You give in-depth info about all other subjects, unlike all the other 10 000 000 videos showing a 15-minute fprintf and fscanf. Keep up the good work

  • @melanymaciasmoran5538
    @melanymaciasmoran5538 Před 2 lety +1

    I have no words to describe how much you have helped me! I am so grateful that this amazing content is open and free. Thank you so much!

  • @jakobfredriksson2272
    @jakobfredriksson2272 Před 3 lety +5

    Your teaching is awesome and I'm very grateful for everything I've learned (and will learn) in this excellent channel. Keep it up!

  • @sgyniguez
    @sgyniguez Před 2 lety +2

    I need to use serialization/deserialization for my project and you explained it in a way that makes it easy. Thank you

  • @gammyhorse
    @gammyhorse Před 2 lety +1

    Honestly, you have the best tutorials about the C programming language, absolutely the best. Congratulations and thank you very much!

    • @lightspd714
      @lightspd714 Před 2 lety +1

      I second this statement. This is top notch content.

  • @mohamedhany2539
    @mohamedhany2539 Před 3 lety

    I came here to understand "JSON Serialization", and I really got the whole picture. Thanks, man :)

  • @finiavanamandresy5460

    Actually this channel is amazing, so glad I found it.

  • @eddiecode100
    @eddiecode100 Před 23 dny

    i'm 30 seconds into the video and i already know this guy is about to spit some facts

  • @husseinyoussef6998
    @husseinyoussef6998 Před 3 lety +2

    Such an underrated channel. I would like to tell you that you make awesome content and I really appreciate your efforts!

  • @_prothegee
    @_prothegee Před 3 lety

    Just in time when decided to dig C and C++ again then saw this
    Thank you, 👍🏽❤️👍🏽

  • @etherweb6796
    @etherweb6796 Před 8 měsíci

    A bit late, but I have done quite a bit of googling on serialization / deserialization in C - was not a lot out there, and this was simple, to the point, and matches my use case almost exactly. (I want a more TOML/systemd unit style format, but that is an easy change to make). Subscribed, and will def be watching more videos.

  • @weelaiyang931
    @weelaiyang931 Před 3 lety +3

    This is great stuff, would love to see it in C++ as well.

  • @zhulikkulik
    @zhulikkulik Před 10 měsíci

    Thank you very much! Exactly what I needed. :)

  • @hanaksi
    @hanaksi Před rokem

    Thank you for the video! It was really helpful for me!

  • @youssefgamal8284
    @youssefgamal8284 Před 2 lety

    Bro you just saved my life . ❤️

  • @taherrezzag9848
    @taherrezzag9848 Před 4 lety

    Thanks man you helped me a lot

  • @matteoiorio5288
    @matteoiorio5288 Před 3 lety +1

    thank you man, very helpfull

  • @grfhumpf
    @grfhumpf Před 4 lety

    Excellent content and very well explained. Already subscribed. o/

  • @anshul4249
    @anshul4249 Před 3 lety

    You are awesome! 🧡

  • @md2perpe
    @md2perpe Před rokem +2

    If one is not going to transfer data between different architectures then one can write the binary data directly from memory to the file and back. The problem with doing it on different architectures is that the byte order of big numbers might differ. One also has to be careful about alignment which might differ.

    • @CodeVault
      @CodeVault  Před 11 měsíci

      Exactly. There are quite a lot of caveats with writing the memory directly. Another thing that could cause issues is pointers in structs

  • @ZenyxPlays061
    @ZenyxPlays061 Před 3 měsíci

    fuckn sane ! what a clarity man.

  • @thatdudefromthecorner7837

    Thank you so much ! this has been very helpful. However I have a question.....is there a way I can get the value of a certain variable such as 'age' and replace with another value...not changing the whole line but only the value stored in 'age'?

    • @CodeVault
      @CodeVault  Před 2 lety +1

      Not really. If the updated value has the same number of characters then you can easily fseek to the position of the age and overwrite, but otherwise, you'll have to rewrite the whole line (or the whole file). Files weren't made to be changed like that, that's why databases exist

  • @jabuci
    @jabuci Před 4 lety +1

    Thanks! What JSON library do you recommend for C? Could you make a video of that too?

  • @salaheddineazzouzi4686

    thanks a lot dude !

  • @nkosanamndiyata9098
    @nkosanamndiyata9098 Před 3 lety

    Excellent Man

  • @necrotikS
    @necrotikS Před 3 lety

    But what if I want to use fwrite() and fread()? I mean, the thing is that I would't be able to choose the format that I want the values to be printed, right? Like you do with fprinf().

    • @CodeVault
      @CodeVault  Před 3 lety +2

      fwrite and fread writes actual memory, not strings. It could work if you write to binary files, just that it won't work if the struct is padded differently or the elements are different sizes (between read and write)

  • @samarthtandale9121
    @samarthtandale9121 Před rokem +1

    I got the dream channel on CZcams 🥳

  • @grimvian
    @grimvian Před rokem

    Thanks again for a great video from a C learner for about a year.
    I ended up using binary files and got rid of ascii 10 and 13 chars in my files.
    Is this code a "problem" or it just a matter of style, because I want to "do stuff" immediately:
    if (fp != NULL) {
    do stuff
    } else {
    oh oh...
    And because of learning C, I am not using the string.h either, but do all editing mostly by pointers and using a debugger a lot.

    • @CodeVault
      @CodeVault  Před rokem +1

      Looks good. But usually it's easier to just check for NULL and return early. That way, you know exactly what the code does at first glance, you don't have to think that this else represents an error situation

  • @ForeverNils
    @ForeverNils Před 2 lety

    Thank you for the video. Would it be better to store data in binary in terms of file size?

    • @CodeVault
      @CodeVault  Před 2 lety +1

      Yes, that's usually the most compact way to store data

    • @ForeverNils
      @ForeverNils Před 2 lety +1

      @@CodeVault Will you do the video on how to convert struct to binary to store data?

  • @ar_soukrat
    @ar_soukrat Před 4 lety

    Hey, I have been reading so many pages to just save my records to a file and reading it again to load data.., you're a lifesaver thanks. but what if we need to store char* instead of char []? do I need to store the number of characters of each string?

    • @CodeVault
      @CodeVault  Před 4 lety

      If name is a char* instead of a char[] then you'd first need to make it point to some allocated memory. So, before calling fscanf, you'd need to do:
      p1.name = malloc(sizeof(char)*100);
      // or make it point to an already existing char array
      p1.name = someCharArray;
      If you want to use malloc, it'd be useful to store the size (in our case: 100) in the person struct BUT not mandatory. It's so you are able to make the memory block larger in case you need larger strings using realloc.

  • @kayeharrissalvacion9915

    woah! amazing...

  • @munawarcheema8991
    @munawarcheema8991 Před 8 měsíci

    Very nice channel. Is there a reason to use a comma rather than spaces or tabs? Just wondering if it makes scanf easier

    • @CodeVault
      @CodeVault  Před 8 měsíci +1

      If you use spaces and the city name has two words in it then the serialization brakes. You usually need to choose a separator that doesn't occur in the data itself (in this case comma was a good choice)

  • @capc0307
    @capc0307 Před 19 dny

    Thank you so much. You explained it really well, but I have a question: What if I only want to save the age and gender in variables, but not the name? I tried to use "{
    \t\"name\": \"[^\"]+\",
    \t\"age\": %d,
    \t\"gender\": \"%c\"
    }" as PERSON_FORMAT_IN and fscanf_s(file, PERSON_FORMAT_IN, &p2.age, &p2.gender) function, but it doesn't work. I think it has something to do with the "+" symbol. I'm not really sure of that. How would I solve it? Thanks in advance.

  • @khamalipowell
    @khamalipowell Před 4 lety

    Good video 👍🏽

  • @leenabaseldeek3900
    @leenabaseldeek3900 Před 4 lety

    hey .. i have a homework its talk about read the data from a file and store the data in array of struct of the same size of file and i should count the number of rows in the file .... can you help me how to store the data in array of struct ??

    • @CodeVault
      @CodeVault  Před 4 lety

      Sure thing, just join here and we'll help you out: discord.code-vault.net

  • @Victor_Marius
    @Victor_Marius Před 3 lety

    Does it work with space and double quote inside the name string?

    • @CodeVault
      @CodeVault  Před 3 lety

      If you replace the %s with %[^,] it should read everything until the next comma (whether or not it's a space,
      or even quotes)

  • @federicosalvetti4286
    @federicosalvetti4286 Před rokem

    Thank you so much! I have a maybe stupid question.
    I've been trying to run this, when I do that from terminal it gives me compilation flags that it can't find fopen_s and fprintf_s and fscanf_s. When I do that from CLion it compiles and execute with a Cmake file, but it doesn't generate the file. How can I fix this? Thanks to anyone who will answer in advance.

    • @CodeVault
      @CodeVault  Před rokem +1

      I'm sorry, in this video I am using the Visual C compiler and that's the one that implements the _s functions. You can simply remove the "_s" and use the usual functions from the standard library. The source code on the website is actually not using the _s functions and you should be able to compile it anywhere except in Visual C: code-vault.net/lesson/1ilh1d0goy:1603733527863

    • @federicosalvetti4286
      @federicosalvetti4286 Před rokem

      @@CodeVault Thanks so much, once again, thank you for the great work you do !

  • @yelimsnusm7551
    @yelimsnusm7551 Před 2 lety

    Hey. Can you tell me why u didnt use fclose?
    do u not need it with fopen_s?

    • @CodeVault
      @CodeVault  Před 2 lety +1

      Oh, you should call fclose. It was an oversight on my part

    • @yelimsnusm7551
      @yelimsnusm7551 Před 2 lety

      Yeah i am kind of a beginner so I wasnt sure. Thanks for help ❤

  • @Ebees_Map
    @Ebees_Map Před 6 měsíci

    Can you please help me? I used serialization and deserialization for saving and loading the game data for a game that I coded in C. The game works fine but, I can't seem to figure out how to actually save and load the game data.

    • @CodeVault
      @CodeVault  Před 5 měsíci

      That seems quite complicated. If you have any specific question I can help

  • @ale-hl8pg
    @ale-hl8pg Před 3 lety +1

    Not sure if anyone can help but i keep getting a segfault when i try to expand the structure by adding more fields
    #include
    #include
    #include
    const char* SER_FORMAT_IN = "(%[^,], %[^,], %c, %d, %d)
    ";
    const char* SER_FORMAT_OUT = "(%s, %s, %c, %d, %d)
    ";
    typedef struct Person
    {
    char name[32];
    char pos[5];
    char gender;
    int year;
    int age;
    } Person;
    int main()
    {
    Person p1 = {
    .name = "Bobby boy",
    .pos = "THR",
    .gender = 'M',
    .year = 3201,
    .age = 992
    };
    Person p2 = {
    .name = "Jorgovanda",
    .pos = "QUA",
    .gender = 'F',
    .year = 2024,
    .age = 24
    };
    Person p3, p4;
    FILE* filePtr;
    fopen_s(&filePtr, "people.txt", "w+");
    if(filePtr == NULL || filePtr == 0)
    {
    perror("Error: ");
    return 1;
    }
    WritePersonToFile(filePtr, p1);
    WritePersonToFile(filePtr, p2);
    fseek(filePtr, 0, SEEK_SET);
    fscanf_s(filePtr, SER_FORMAT_IN, p3.name, 32, p3.pos, 5, &p3.gender, &p3.year, &p3.age);
    fscanf_s(filePtr, SER_FORMAT_IN, p4.name, 32, p4.pos, 5, &p4.gender, &p4.year, &p4.age);
    // fscanf_s(filePtr, SER_FORMAT_IN, &p3.name, 20, &p3.pos, 5, &p3.gender, &p3.year, &p3.age);
    ReadPersonStatistics(p3);
    ReadPersonStatistics(p4);
    fclose(filePtr);
    return 0;
    }
    void WritePersonToFile(FILE* filePtr, Person p)
    {
    fprintf_s(filePtr, SER_FORMAT_OUT, p.name, p.pos, p.gender, p.year, p.age);
    }
    void ReadSinglePersonFromFile_p(FILE* filePtr, Person* p)
    {
    fscanf_s(filePtr, SER_FORMAT_IN, p->name, 32, p->pos, 5, p->gender, p->year, p->age);
    }
    void ReadPersonStatistics(Person p)
    {
    printf("Name: %s
    Pos: %s
    Gender: %c
    Year: %d
    Age: %d
    ", p.name, p.pos, p.gender, p.year, p.age);
    }
    the first fscanf_s always seems to throw a segfault when i add more fields to the structure...

    • @CodeVault
      @CodeVault  Před 3 lety +1

      If you're reading a character using fscanf_s you have to specify the size:
      This:
      fscanf_s(filePtr, SER_FORMAT_IN, p3.name, 32, p3.pos, 5, &p3.gender, 1, &p3.year, &p3.age);
      Instead of:
      fscanf_s(filePtr, SER_FORMAT_IN, p3.name, 32, p3.pos, 5, &p3.gender, &p3.year, &p3.age);
      Otherwise I suggest you just use the normal fscanf functions by adding this at the beginning of your source:
      #define _CRT_SECURE_NO_WARNINGS
      Hope that solves the issue.

    • @ale-hl8pg
      @ale-hl8pg Před 3 lety

      @@CodeVault Thanks, i thought it was only for char arrays

  • @KFlorent13
    @KFlorent13 Před 2 lety

    Can we use a preprocessor macro for the PERSON_FORMAT_OUT and IN ?

  • @senhordoobvio178
    @senhordoobvio178 Před 3 měsíci

    What if I have inside the StructA a pointer to StructB, and may have a bunch of StructA pointing to the same StructB. How could I serialize and deserialize that?

    • @CodeVault
      @CodeVault  Před 3 měsíci

      I would probably serialize the contents of structB as well. Either having them duplicated or have some mechanism that deduplicates these writes

  • @narasarajv5278
    @narasarajv5278 Před 3 lety

    I need to load Exel file values into array or buffer instead of text file data. By C program how we can do please explain the program?

    • @CodeVault
      @CodeVault  Před 3 lety

      It is quite difficult to read/write to Excel files, especially in C... This library might help you with that: github.com/libxls/libxls

    • @narasarajv5278
      @narasarajv5278 Před 3 lety

      @@CodeVault Thank you so much...

  • @syntaxed2
    @syntaxed2 Před 3 lety +1

    Will this work on another machine like some older machine allocates memory differently?

    • @CodeVault
      @CodeVault  Před 3 lety

      The code is portable enough I think (depends on the machine though). On what machine you want to run this?

    • @syntaxed2
      @syntaxed2 Před 3 lety

      @@CodeVault Sorry, I was thinking about memory padding etc? Will there be any issues between 32/64bit?

    • @CodeVault
      @CodeVault  Před 3 lety

      @@syntaxed2 No, since you're just reading/writing text files. It would be an issue if you would write the struct in binary

    • @syntaxed2
      @syntaxed2 Před 3 lety

      @@CodeVault ahh I see, thanks for the video & info :D subbed!

  • @vineeshapudota6491
    @vineeshapudota6491 Před 3 lety

    Thanks for this video. I had one doubt instead of copying the data into file, I need to copy into a buffer. Can you please suggest me the way. Thanks in advance

    • @CodeVault
      @CodeVault  Před 3 lety +1

      Just use sprintf instead of fprintf. The difference is sprintf takes in a buffer as its first parameter.

    • @vineeshapudota6491
      @vineeshapudota6491 Před 3 lety

      @@CodeVault Thanks for your reply. I want to store 10 records data in the buffer. i am able to store only one record with sprintf. TIA

    • @CodeVault
      @CodeVault  Před 3 lety +1

      @@vineeshapudota6491 Call sprintf multiple times if needed and increment the buffer pointer with the previously returned value from sprintf.
      int charsWritten = sprintf(buf, "%d
      ", x1);
      charsWritten += sprintf(buf + charsWritten, "%d
      ", x2);
      charsWritten += sprintf(buf + charsWritten, "%d
      ", x3);
      // ... and so on

    • @vineeshapudota6491
      @vineeshapudota6491 Před 3 lety

      @@CodeVault Inside for loop I am calling sprintf and scanf. TIA

    • @CodeVault
      @CodeVault  Před 3 lety

      @@vineeshapudota6491 Should work regardless of how the data is read in the variables

  • @mikeehxz2875
    @mikeehxz2875 Před 2 lety

    you need to put fopen_s or you can put fopen only?

    • @CodeVault
      @CodeVault  Před 2 lety +1

      You can use fopen too, just make sure you use it properly:
      FILE* f;
      fopen_s(&f, "test", "r");
      But with fopen it is:
      FILE* f;
      f = fopen("test", "r");

  • @davidbarnwellutech4663

    Where can i find the code for this? There's a syntax error in my JSON FORMAT_OUT string but I can't see where it is. I'd love to compare your code so I could more easily find my error.

    • @CodeVault
      @CodeVault  Před 2 lety +1

      All the code for all the videos can be found on the CodeVault website, here's the link for this video: code-vault.net/lesson/1ilh1d0goy:1603733527863

    • @davidbarnwell_virtual_clas6729
      @davidbarnwell_virtual_clas6729 Před 2 lety

      @@CodeVault Thank you

  • @chriscruz429
    @chriscruz429 Před 2 lety

    The Vincent Willem van Gogh of programming.

  • @armandolopezespinoza3823

    Es correcto usar fscanf de la forma q usaste en el otro video while(){sscanf and fgets}

  • @nimeshnelanga8780
    @nimeshnelanga8780 Před 3 lety

    When I typed the same code in VS2019 it gives me an error, that .name = "And", is wrong by highlighting it with red, I'm so confused

    • @CodeVault
      @CodeVault  Před 3 lety +1

      Ahh, just initialize like normal... I don't think MSVC supports that yet, their compiler is usually way behind in features. So just do instead:
      Person p1;
      p1.name = "Andrew";
      p1.age = 22;
      p1.gender = 'M';

    • @nimeshnelanga8780
      @nimeshnelanga8780 Před 3 lety

      Thnx for the reply. Assigning "string" to a char[ ] also doesn't seem to work, what compiler do you use.

    • @CodeVault
      @CodeVault  Před 3 lety +1

      Oh, right... in the video it works because compiler magic.
      You can just do strcpy(p1.name, "Andrew"); instead

  • @bananamonke630
    @bananamonke630 Před 2 lety

    can i store data in a byte array instead of a file using this method

    • @CodeVault
      @CodeVault  Před 2 lety

      Yes, of course. Instead of fprintf and fscanf, you can use sprintf and sscanf to read/write into a string (which is practically a byte array)

  • @unknow2096
    @unknow2096 Před 3 lety

    Hi
    If i have many persons should say that i have them already in a file okay?
    My question is if i add a new person that a different age then the other and i want to arrange in ascending order how i can do that? Im still not figuring out how to do it can you help me?

    • @CodeVault
      @CodeVault  Před 3 lety

      Here are the basic steps:
      1) Read the file with all its contents in an array
      2) Add the new person to that array
      3) Sort that array
      4) Delete all the contents of the file
      5) Write the whole array to the file

    • @unknow2096
      @unknow2096 Před 3 lety

      @@CodeVault i did the exact thing you're telling but the file that is being created have nothing in it! And if i wanted to direct edit the file using notepad for example if i use the programm it delete what's inside of it

    • @unknow2096
      @unknow2096 Před 3 lety

      @@CodeVault i will send you the code that i made just a second

    • @unknow2096
      @unknow2096 Před 3 lety

      @@CodeVault void cree ()
      {
      FILE *f;
      personne a[10];
      int i=1,j=0,*v;
      f = fopen("PERSONNE.txt","w");
      printf("Donner le nom de la personne %d :
      ",i);
      gets(a[j].nom);
      fprintf(f,"Nom \t Age");
      while(strcmp(a[j].nom," ")!=0 )
      {
      printf("Donner l'age de la personne %d :
      ",i);
      scanf("%d", &a[j].age);
      i++;
      fprintf(f,"%s \t %d",a[j].nom,a[j].age);
      j++;
      printf("Donner le nom de la personne %d :
      ",i);
      fflush(stdin);
      gets(a[j].nom);
      }
      *v=j;
      fclose(f);
      }

    • @unknow2096
      @unknow2096 Před 3 lety

      @@CodeVault this is the function causing the problem that's creating the file because i tried it alone in another c programm alone in a main function and it's doing the same thing making the file but not copying anything inside of it

  • @mongraal2272
    @mongraal2272 Před 2 lety

    But how we do this with an array of structures?Can u please make a video

    • @CodeVault
      @CodeVault  Před 2 lety

      You can simply use this technique but in a for loop where you read/write each struct

  • @fooel1993
    @fooel1993 Před 2 lety

    how can i read the nextline in this file?

    • @CodeVault
      @CodeVault  Před 2 lety

      Using fgets you can read the next line after the cursor. Using fseek you can set where you want the cursor to be

  • @armandolopezespinoza3823

    Como se haria con un array de objectos? me urge un ejemplo de video

    • @CodeVault
      @CodeVault  Před 4 lety

      There's this one video for reading an array of structs: czcams.com/video/shYMgRcjm5A/video.html
      You can use more or less the same method to write the array in the file

    • @armandolopezespinoza3823
      @armandolopezespinoza3823 Před 4 lety

      @@CodeVault Muchas Gracias, justo queria ese json con array(las bibliotecas de internet no me ayudan), pero revisare tu video

  • @mba2ceo
    @mba2ceo Před rokem

    please write array of classes to binary file then close the program restart the program and read class members into array of classes from that binary file

    • @CodeVault
      @CodeVault  Před rokem

      Array of classes should be similar to what is shown in the video, except you write/read multiple times

  • @12_6__2_3
    @12_6__2_3 Před rokem

    why char name[20] and not string name please help me?

    • @CodeVault
      @CodeVault  Před rokem +1

      There's no such data type as "string" in C
      There is however std::string from the standard library in C++

  • @betterlifeexe4378
    @betterlifeexe4378 Před 3 lety +1

    Help? Trying to figure out how to make use of multiple xml sterilized objects: stackoverflow.com/questions/65964079/how-to-serialize-deserialize-several-objects-to-a-single-xml-file-in-c

    • @CodeVault
      @CodeVault  Před 3 lety +1

      It's not an easy task. You'll have to parse each line individually I think and construct a stack of open tags that are then removed on encountering a closing tag (and in between you save the relevant information). Or you can just use a library: www.jclark.com/xml/expat.html or www.xmlsoft.org/

    • @betterlifeexe4378
      @betterlifeexe4378 Před 3 lety

      @@CodeVault Thank you, This is my third answer, got them all today, and there all different solutions : )

  • @davidpacho6540
    @davidpacho6540 Před měsícem

    22 😅

  • @GGG-hh5jo
    @GGG-hh5jo Před 3 lety

    #define MAX 32
    Struct WEAPON
    { char[MAX];
    };
    WEAPON weapon[MAX] = {};
    Gets error saying
    Expected ';' before 'weapon'

    • @CodeVault
      @CodeVault  Před 3 lety +1

      If you don't use typedef when defining the struct you'll have to use the "struct" keyword every time. Like this:
      struct WEAPON weapon[MAX] = {};
      Here's a video elaborating on this: code-vault.net/lesson/x8a6oj884e:1603733520791

  • @ItsD3vil
    @ItsD3vil Před měsícem

    I find this approach the easiest, yes, but not the most reliable. I wish you had used a union or packed the struct, then dumped it to a file and reloaded it.

    • @CodeVault
      @CodeVault  Před měsícem

      There are some videos on the topic, although they don't go into much detail: code-vault.net/lesson/dc7v6ej05a:1603733523111
      code-vault.net/lesson/c6fxa8ef2y:1603733523174

    • @ItsD3vil
      @ItsD3vil Před měsícem

      ​@@CodeVault Thanks. I've read them; they are good, but they miss the part about packing the struct. The compiler adds some alignment that differs from one compiler to another.
      I hope if you make a topic for this in your channel for people to understand more the power of C and what they also can do with it.

    • @CodeVault
      @CodeVault  Před měsícem

      @@ItsD3vil I noted it down. Not sure when I will get to it

  • @user-xe3bf6pj6j
    @user-xe3bf6pj6j Před rokem +2

    Only two genders? (im just kidding)

  • @Nockoutz1
    @Nockoutz1 Před 4 lety

    Helped me very much , thank you!