Answer the question
In order to leave comments, you need to log in
How to write and then read a structure from a binary file?
The essence is this, I write ala "Archiver", the essence is this, there is a structure:
struct File {
char *fileName;
char *fileContent;
unsigneв int fileSize;
}
Answer the question
In order to leave comments, you need to log in
There is no VARIABLE structure, the size of all data types is fixed at compile time. Memory must be allocated separately for each field of the structure, and each field will have to be saved / loaded separately. Those. in the file you write "fileName size", "fileName bytes", "fileContent size", "fileContent bytes". I’ll note right away that unsigned int for the file size limits the file size to 4 gigabytes, it’s better to use int64_t right away (I don’t know how it is customary to call it in C).
For reading / writing, you need to open it in binary form (for this task), and not in text.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question