Reading/Writing structs to files (aka Serialization)

Поделиться
HTML-код
  • Опубликовано: 20 апр 2020
  • Check out our Discord server: / discord

Комментарии • 123

  • @kalinmarinov5268
    @kalinmarinov5268 4 года назад +33

    Hey, that is the most in-depth explanation for serialization and struct files i have found on RUclips 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 2 года назад +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 2 года назад +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 2 года назад +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 2 года назад +1

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

    • @lightspd714
      @lightspd714 2 года назад +1

      I second this statement. This is top notch content.

  • @mohamedhany2539
    @mohamedhany2539 3 года назад

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

  • @finiavanamandresy5460
    @finiavanamandresy5460 Год назад

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

  • @etherweb6796
    @etherweb6796 8 месяцев назад

    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.

  • @zhulikkulik
    @zhulikkulik 10 месяцев назад

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

  • @husseinyoussef6998
    @husseinyoussef6998 3 года назад +2

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

  • @_prothegee
    @_prothegee 3 года назад

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

  • @weelaiyang931
    @weelaiyang931 3 года назад +3

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

  • @hanaksi
    @hanaksi Год назад

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

  • @grfhumpf
    @grfhumpf 3 года назад

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

  • @md2perpe
    @md2perpe 11 месяцев назад +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  10 месяцев назад

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

  • @youssefgamal8284
    @youssefgamal8284 2 года назад

    Bro you just saved my life . ❤️

  • @taherrezzag9848
    @taherrezzag9848 4 года назад

    Thanks man you helped me a lot

  • @ZenyxPlays061
    @ZenyxPlays061 2 месяца назад

    fuckn sane ! what a clarity man.

  • @matteoiorio5288
    @matteoiorio5288 3 года назад +1

    thank you man, very helpfull

  • @jabuci
    @jabuci 4 года назад +1

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

  • @anshul4249
    @anshul4249 3 года назад

    You are awesome! 🧡

  • @thatdudefromthecorner7837
    @thatdudefromthecorner7837 2 года назад

    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  2 года назад +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

  • @samarthtandale9121
    @samarthtandale9121 Год назад +1

    I got the dream channel on RUclips 🥳

  • @grimvian
    @grimvian Год назад

    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  Год назад +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

  • @nkosanamndiyata9098
    @nkosanamndiyata9098 3 года назад

    Excellent Man

  • @necrotikS
    @necrotikS 3 года назад

    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  3 года назад +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)

  • @salaheddineazzouzi4686
    @salaheddineazzouzi4686 3 года назад

    thanks a lot dude !

  • @munawarcheema8991
    @munawarcheema8991 8 месяцев назад

    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  7 месяцев назад +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)

  • @kayeharrissalvacion9915
    @kayeharrissalvacion9915 3 года назад

    woah! amazing...

  • @ForeverNils
    @ForeverNils 2 года назад

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

    • @CodeVault
      @CodeVault  2 года назад +1

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

    • @ForeverNils
      @ForeverNils 2 года назад +1

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

  • @senhordoobvio178
    @senhordoobvio178 2 месяца назад

    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  2 месяца назад

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

  • @Ebees_Map
    @Ebees_Map 5 месяцев назад

    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  4 месяца назад

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

  • @leenabaseldeek3900
    @leenabaseldeek3900 4 года назад

    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  4 года назад

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

  • @ale-hl8pg
    @ale-hl8pg 3 года назад +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  3 года назад +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 3 года назад

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

  • @khamalipowell
    @khamalipowell 4 года назад

    Good video 👍🏽

  • @Victor_Marius
    @Victor_Marius 3 года назад

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

    • @CodeVault
      @CodeVault  3 года назад

      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 Год назад

    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  Год назад +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 Год назад

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

  • @chriscruz429
    @chriscruz429 2 года назад

    The Vincent Willem van Gogh of programming.

  • @KFlorent13
    @KFlorent13 2 года назад

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

  • @syntaxed2
    @syntaxed2 3 года назад +1

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

    • @CodeVault
      @CodeVault  3 года назад

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

    • @syntaxed2
      @syntaxed2 3 года назад

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

    • @CodeVault
      @CodeVault  3 года назад

      @@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 3 года назад

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

  • @narasarajv5278
    @narasarajv5278 3 года назад

    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  3 года назад

      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 3 года назад

      @@CodeVault Thank you so much...

  • @vineeshapudota6491
    @vineeshapudota6491 3 года назад

    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  3 года назад +1

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

    • @vineeshapudota6491
      @vineeshapudota6491 3 года назад

      @@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  3 года назад +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 3 года назад

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

    • @CodeVault
      @CodeVault  3 года назад

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

  • @yelimsnusm7551
    @yelimsnusm7551 2 года назад

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

    • @CodeVault
      @CodeVault  2 года назад +1

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

    • @yelimsnusm7551
      @yelimsnusm7551 2 года назад

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

  • @bananamonke630
    @bananamonke630 Год назад

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

    • @CodeVault
      @CodeVault  Год назад

      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)

  • @davidbarnwellutech4663
    @davidbarnwellutech4663 2 года назад

    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  2 года назад +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 2 года назад

      @@CodeVault Thank you

  • @nimeshnelanga8780
    @nimeshnelanga8780 3 года назад

    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  3 года назад +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 3 года назад

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

    • @CodeVault
      @CodeVault  3 года назад +1

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

  • @mikeehxz2875
    @mikeehxz2875 2 года назад

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

    • @CodeVault
      @CodeVault  2 года назад +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");

  • @ar_soukrat
    @ar_soukrat 4 года назад

    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  4 года назад

      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.

  • @armandolopezespinoza3823
    @armandolopezespinoza3823 3 года назад

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

  • @unknow2096
    @unknow2096 3 года назад

    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  3 года назад

      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 3 года назад

      @@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 3 года назад

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

    • @unknow2096
      @unknow2096 3 года назад

      @@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 3 года назад

      @@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

  • @armandolopezespinoza3823
    @armandolopezespinoza3823 3 года назад

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

    • @CodeVault
      @CodeVault  3 года назад

      There's this one video for reading an array of structs: ruclips.net/video/shYMgRcjm5A/видео.html
      You can use more or less the same method to write the array in the file

    • @armandolopezespinoza3823
      @armandolopezespinoza3823 3 года назад

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

  • @mongraal2272
    @mongraal2272 2 года назад

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

    • @CodeVault
      @CodeVault  2 года назад

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

  • @fooel1993
    @fooel1993 2 года назад

    how can i read the nextline in this file?

    • @CodeVault
      @CodeVault  2 года назад

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

  • @12_6__2_3
    @12_6__2_3 Год назад

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

    • @CodeVault
      @CodeVault  Год назад +1

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

  • @mba2ceo
    @mba2ceo Год назад

    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  Год назад

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

  • @betterlifeexe4378
    @betterlifeexe4378 3 года назад +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  3 года назад +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 3 года назад

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

  • @davidpacho6540
    @davidpacho6540 6 дней назад

    22 😅

  • @GGG-hh5jo
    @GGG-hh5jo 3 года назад

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

    • @CodeVault
      @CodeVault  3 года назад +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 7 дней назад

    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  5 дней назад

      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 5 дней назад

      ​@@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  5 дней назад

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

  • @user-xe3bf6pj6j
    @user-xe3bf6pj6j Год назад +2

    Only two genders? (im just kidding)

  • @Nockoutz1
    @Nockoutz1 4 года назад

    Helped me very much , thank you!