Hey, that is the most in-depth explanation for serialization and struct files i have found on KZfaq 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
@melanymaciasmoran55382 жыл бұрын
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!
@jakobfredriksson22723 жыл бұрын
Your teaching is awesome and I'm very grateful for everything I've learned (and will learn) in this excellent channel. Keep it up!
@sgyniguez2 жыл бұрын
I need to use serialization/deserialization for my project and you explained it in a way that makes it easy. Thank you
@gammyhorse2 жыл бұрын
Honestly, you have the best tutorials about the C programming language, absolutely the best. Congratulations and thank you very much!
@lightspd7142 жыл бұрын
I second this statement. This is top notch content.
@mohamedhany25393 жыл бұрын
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.
@etherweb67969 ай бұрын
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.
@weelaiyang9313 жыл бұрын
This is great stuff, would love to see it in C++ as well.
@husseinyoussef69983 жыл бұрын
Such an underrated channel. I would like to tell you that you make awesome content and I really appreciate your efforts!
@_prothegee3 жыл бұрын
Just in time when decided to dig C and C++ again then saw this Thank you, 👍🏽❤️👍🏽
@zhulikkulik11 ай бұрын
Thank you very much! Exactly what I needed. :)
@hanaksi Жыл бұрын
Thank you for the video! It was really helpful for me!
@md2perpe Жыл бұрын
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 Жыл бұрын
Exactly. There are quite a lot of caveats with writing the memory directly. Another thing that could cause issues is pointers in structs
@taherrezzag98484 жыл бұрын
Thanks man you helped me a lot
@grfhumpf4 жыл бұрын
Excellent content and very well explained. Already subscribed. o/
@youssefgamal82842 жыл бұрын
Bro you just saved my life . ❤️
@matteoiorio52883 жыл бұрын
thank you man, very helpfull
@anshul42493 жыл бұрын
You are awesome! 🧡
@eddiecode100Ай бұрын
i'm 30 seconds into the video and i already know this guy is about to spit some facts
@jabuci4 жыл бұрын
Thanks! What JSON library do you recommend for C? Could you make a video of that too?
@CodeVault4 жыл бұрын
I'll look into it
@nkosanamndiyata90983 жыл бұрын
Excellent Man
@thatdudefromthecorner78372 жыл бұрын
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'?
@CodeVault2 жыл бұрын
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
@necrotikS3 жыл бұрын
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().
@CodeVault3 жыл бұрын
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)
@salaheddineazzouzi46863 жыл бұрын
thanks a lot dude !
@khamalipowell4 жыл бұрын
Good video 👍🏽
@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 Жыл бұрын
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
@ZenyxPlays0614 ай бұрын
fuckn sane ! what a clarity man.
@leenabaseldeek39004 жыл бұрын
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 ??
@CodeVault4 жыл бұрын
Sure thing, just join here and we'll help you out: discord.code-vault.net
@capc0307Ай бұрын
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.
@Victor_Marius3 жыл бұрын
Does it work with space and double quote inside the name string?
@CodeVault3 жыл бұрын
If you replace the %s with %[^,] it should read everything until the next comma (whether or not it's a space, or even quotes)
@munawarcheema89919 ай бұрын
Very nice channel. Is there a reason to use a comma rather than spaces or tabs? Just wondering if it makes scanf easier
@CodeVault9 ай бұрын
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)
@kayeharrissalvacion99153 жыл бұрын
woah! amazing...
@ForeverNils2 жыл бұрын
Thank you for the video. Would it be better to store data in binary in terms of file size?
@CodeVault2 жыл бұрын
Yes, that's usually the most compact way to store data
@ForeverNils2 жыл бұрын
@@CodeVault Will you do the video on how to convert struct to binary to store data?
@armandolopezespinoza38234 жыл бұрын
Es correcto usar fscanf de la forma q usaste en el otro video while(){sscanf and fgets}
@ale-hl8pg3 жыл бұрын
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...
@CodeVault3 жыл бұрын
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-hl8pg3 жыл бұрын
@@CodeVault Thanks, i thought it was only for char arrays
@KFlorent132 жыл бұрын
Can we use a preprocessor macro for the PERSON_FORMAT_OUT and IN ?
@CodeVault2 жыл бұрын
Yes, of course!
@Ebees_Map6 ай бұрын
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.
@CodeVault6 ай бұрын
That seems quite complicated. If you have any specific question I can help
@senhordoobvio1784 ай бұрын
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?
@CodeVault3 ай бұрын
I would probably serialize the contents of structB as well. Either having them duplicated or have some mechanism that deduplicates these writes
@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 Жыл бұрын
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 Жыл бұрын
@@CodeVault Thanks so much, once again, thank you for the great work you do !
@bananamonke6302 жыл бұрын
can i store data in a byte array instead of a file using this method
@CodeVault2 жыл бұрын
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)
@narasarajv52783 жыл бұрын
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?
@CodeVault3 жыл бұрын
It is quite difficult to read/write to Excel files, especially in C... This library might help you with that: github.com/libxls/libxls
@narasarajv52783 жыл бұрын
@@CodeVault Thank you so much...
@syntaxed23 жыл бұрын
Will this work on another machine like some older machine allocates memory differently?
@CodeVault3 жыл бұрын
The code is portable enough I think (depends on the machine though). On what machine you want to run this?
@syntaxed23 жыл бұрын
@@CodeVault Sorry, I was thinking about memory padding etc? Will there be any issues between 32/64bit?
@CodeVault3 жыл бұрын
@@syntaxed2 No, since you're just reading/writing text files. It would be an issue if you would write the struct in binary
@syntaxed23 жыл бұрын
@@CodeVault ahh I see, thanks for the video & info :D subbed!
@ar_soukrat4 жыл бұрын
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?
@CodeVault4 жыл бұрын
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.
@mikeehxz28752 жыл бұрын
you need to put fopen_s or you can put fopen only?
@CodeVault2 жыл бұрын
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");
@yelimsnusm75512 жыл бұрын
Hey. Can you tell me why u didnt use fclose? do u not need it with fopen_s?
@CodeVault2 жыл бұрын
Oh, you should call fclose. It was an oversight on my part
@yelimsnusm75512 жыл бұрын
Yeah i am kind of a beginner so I wasnt sure. Thanks for help ❤
@vineeshapudota64914 жыл бұрын
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
@CodeVault3 жыл бұрын
Just use sprintf instead of fprintf. The difference is sprintf takes in a buffer as its first parameter.
@vineeshapudota64913 жыл бұрын
@@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
@CodeVault3 жыл бұрын
@@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
@vineeshapudota64913 жыл бұрын
@@CodeVault Inside for loop I am calling sprintf and scanf. TIA
@CodeVault3 жыл бұрын
@@vineeshapudota6491 Should work regardless of how the data is read in the variables
@davidbarnwellutech46632 жыл бұрын
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.
@CodeVault2 жыл бұрын
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_clas67292 жыл бұрын
@@CodeVault Thank you
@mongraal22722 жыл бұрын
But how we do this with an array of structures?Can u please make a video
@CodeVault2 жыл бұрын
You can simply use this technique but in a for loop where you read/write each struct
@nimeshnelanga87803 жыл бұрын
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
@CodeVault3 жыл бұрын
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';
@nimeshnelanga87803 жыл бұрын
Thnx for the reply. Assigning "string" to a char[ ] also doesn't seem to work, what compiler do you use.
@CodeVault3 жыл бұрын
Oh, right... in the video it works because compiler magic. You can just do strcpy(p1.name, "Andrew"); instead
@armandolopezespinoza38234 жыл бұрын
Como se haria con un array de objectos? me urge un ejemplo de video
@CodeVault4 жыл бұрын
There's this one video for reading an array of structs: kzfaq.info/get/bejne/qc6JgMqDyNDRZXU.html You can use more or less the same method to write the array in the file
@armandolopezespinoza38234 жыл бұрын
@@CodeVault Muchas Gracias, justo queria ese json con array(las bibliotecas de internet no me ayudan), pero revisare tu video
@12_6__2_3 Жыл бұрын
why char name[20] and not string name please help me?
@CodeVault Жыл бұрын
There's no such data type as "string" in C There is however std::string from the standard library in C++
@samarthtandale9121 Жыл бұрын
I got the dream channel on KZfaq 🥳
@fooel19932 жыл бұрын
how can i read the nextline in this file?
@CodeVault2 жыл бұрын
Using fgets you can read the next line after the cursor. Using fseek you can set where you want the cursor to be
@unknow20963 жыл бұрын
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?
@CodeVault3 жыл бұрын
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
@unknow20963 жыл бұрын
@@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
@unknow20963 жыл бұрын
@@CodeVault i will send you the code that i made just a second
@unknow20963 жыл бұрын
@@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); }
@unknow20963 жыл бұрын
@@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
@chriscruz4292 жыл бұрын
The Vincent Willem van Gogh of programming.
@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 Жыл бұрын
Array of classes should be similar to what is shown in the video, except you write/read multiple times
@betterlifeexe43783 жыл бұрын
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
@CodeVault3 жыл бұрын
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/
@betterlifeexe43783 жыл бұрын
@@CodeVault Thank you, This is my third answer, got them all today, and there all different solutions : )
@GGG-hh5jo3 жыл бұрын
#define MAX 32 Struct WEAPON { char[MAX]; }; WEAPON weapon[MAX] = {}; Gets error saying Expected ';' before 'weapon'
@CodeVault3 жыл бұрын
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
@davidpacho6540Ай бұрын
22 😅
@ItsD3vilАй бұрын
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Ай бұрын
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Ай бұрын
@@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Ай бұрын
@@ItsD3vil I noted it down. Not sure when I will get to it