Hey, that is the most in-depth explanation for serialization and struct files i have found on UA-cam 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
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.
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.
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)
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?
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.
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.
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
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'?
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
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.
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?
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.
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().
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)
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
@@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
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.
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
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.
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?
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
@@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
@@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
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 ??
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.
There's this one video for reading an array of structs: ua-cam.com/video/shYMgRcjm5A/v-deo.html You can use more or less the same method to write the array in the file
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';
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
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/
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.
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
@@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.
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
Hey, that is the most in-depth explanation for serialization and struct files i have found on UA-cam 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
Your teaching is awesome and I'm very grateful for everything I've learned (and will learn) in this excellent channel. Keep it up!
I need to use serialization/deserialization for my project and you explained it in a way that makes it easy. Thank you
Honestly, you have the best tutorials about the C programming language, absolutely the best. Congratulations and thank you very much!
I second this statement. This is top notch content.
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!
Actually this channel is amazing, so glad I found it.
I came here to understand "JSON Serialization", and I really got the whole picture. Thanks, man :)
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.
Exactly. There are quite a lot of caveats with writing the memory directly. Another thing that could cause issues is pointers in structs
This is great stuff, would love to see it in C++ as well.
Thank you so much for making these great videos!
Such an underrated channel. I would like to tell you that you make awesome content and I really appreciate your efforts!
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.
i'm 30 seconds into the video and i already know this guy is about to spit some facts
Thank you very much! Exactly what I needed. :)
fuckn sane ! what a clarity man.
Thank you for the video! It was really helpful for me!
The Vincent Willem van Gogh of programming.
Very nice channel. Is there a reason to use a comma rather than spaces or tabs? Just wondering if it makes scanf easier
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)
Thanks! What JSON library do you recommend for C? Could you make a video of that too?
I'll look into it
Just in time when decided to dig C and C++ again then saw this
Thank you, 👍🏽❤️👍🏽
Thank you for the video. Would it be better to store data in binary in terms of file size?
Yes, that's usually the most compact way to store data
@@CodeVault Will you do the video on how to convert struct to binary to store data?
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?
I would probably serialize the contents of structB as well. Either having them duplicated or have some mechanism that deduplicates these writes
I got the dream channel on UA-cam 🥳
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.
Here are the format specifiers for scanf: cplusplus.com/reference/cstdio/scanf/
+ will probably match a literal '+' character
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.
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
Hey. Can you tell me why u didnt use fclose?
do u not need it with fopen_s?
Oh, you should call fclose. It was an oversight on my part
Yeah i am kind of a beginner so I wasnt sure. Thanks for help ❤
you need to put fopen_s or you can put fopen only?
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");
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'?
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
Will this work on another machine like some older machine allocates memory differently?
The code is portable enough I think (depends on the machine though). On what machine you want to run this?
@@CodeVault Sorry, I was thinking about memory padding etc? Will there be any issues between 32/64bit?
@@syntaxed2 No, since you're just reading/writing text files. It would be an issue if you would write the struct in binary
@@CodeVault ahh I see, thanks for the video & info :D subbed!
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.
That seems quite complicated. If you have any specific question I can help
Bro you just saved my life . ❤️
why char name[20] and not string name please help me?
There's no such data type as "string" in C
There is however std::string from the standard library in C++
Excellent content and very well explained. Already subscribed. o/
can i store data in a byte array instead of a file using this method
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)
thank you man, very helpfull
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?
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.
Can we use a preprocessor macro for the PERSON_FORMAT_OUT and IN ?
Yes, of course!
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().
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)
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
Just use sprintf instead of fprintf. The difference is sprintf takes in a buffer as its first parameter.
@@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
@@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
@@CodeVault Inside for loop I am calling sprintf and scanf. TIA
@@vineeshapudota6491 Should work regardless of how the data is read in the variables
But how we do this with an array of structures?Can u please make a video
You can simply use this technique but in a for loop where you read/write each struct
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.
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
@@CodeVault Thanks so much, once again, thank you for the great work you do !
Does it work with space and double quote inside the name string?
If you replace the %s with %[^,] it should read everything until the next comma (whether or not it's a space,
or even quotes)
You are awesome! 🧡
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.
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
@@CodeVault Thank you
how can i read the nextline in this file?
Using fgets you can read the next line after the cursor. Using fseek you can set where you want the cursor to be
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?
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
@@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
@@CodeVault i will send you the code that i made just a second
@@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);
}
@@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
Thanks man you helped me a lot
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?
It is quite difficult to read/write to Excel files, especially in C... This library might help you with that: github.com/libxls/libxls
@@CodeVault Thank you so much...
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 ??
Sure thing, just join here and we'll help you out: discord.code-vault.net
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...
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.
@@CodeVault Thanks, i thought it was only for char arrays
thanks a lot dude !
Como se haria con un array de objectos? me urge un ejemplo de video
There's this one video for reading an array of structs: ua-cam.com/video/shYMgRcjm5A/v-deo.html
You can use more or less the same method to write the array in the file
@@CodeVault Muchas Gracias, justo queria ese json con array(las bibliotecas de internet no me ayudan), pero revisare tu video
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
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';
Thnx for the reply. Assigning "string" to a char[ ] also doesn't seem to work, what compiler do you use.
Oh, right... in the video it works because compiler magic.
You can just do strcpy(p1.name, "Andrew"); instead
woah! amazing...
Excellent Man
Es correcto usar fscanf de la forma q usaste en el otro video while(){sscanf and fgets}
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
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/
@@CodeVault Thank you, This is my third answer, got them all today, and there all different solutions : )
Good video 👍🏽
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.
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
@@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.
@@ItsD3vil I noted it down. Not sure when I will get to it
Helped me very much , thank you!
22 😅
#define MAX 32
Struct WEAPON
{ char[MAX];
};
WEAPON weapon[MAX] = {};
Gets error saying
Expected ';' before 'weapon'
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
Only two genders? (im just kidding)
ts aint funny