C - Save / load pointer data to a file

First, I apologize if this question was asked earlier or has an obvious obvious solution that I do not see. I found a similar question, but I believe that what I ask goes a little further than what was previously asked.

I have a structure as follows:

typedef struct {
        int id;
        char *title;
        char *body;
} journal_entry;

Q: How to write and load the contents of a pointer to memory in C (not C ++) without using fixed lengths?

Am I really mistaken in the fact that by writing titleor bodyto a file, I get garbage data, and not the information that I saved? I do not know the size that will be titleor the bodylog entries, and the size can vary significantly from write to write.

In my own reading, I need the dereference pointers and fwriteeach part of the structure to be separate. But I'm not sure how to track data and structures without any problems, especially for large files. In addition, if these are not the only elements that I intend to store in the file (for example, I can include small images later, I'm not sure how to arrange the file structure for convenience.

Another (perhaps perceived) problem is that I used mallocto allocate memory for a line for body / write when loading data, how do I know how much memory is allocated for a line when I want to load a record again? Do I need to expand my structure to include int body_lenand int title_len?

Guidance or suggestions would be greatly appreciated.

+4
3

( Linux, )

, , serialization ( wikipedia) - :

- , .

I/O

, . %p fprintf (3) fscanf (3) ( write read , , , intptr_t. (, 0x1234F580...), , (, - ASLR).

, JSON ( ) , YAML (, , , , s-exprs). ( Unix ​​ 1980 ) (, XDR, ASN/1,...). (HTTP, SMTP, FTP, JSONRPC....)

, I/O , , w.r.t. IO (. )

(, a struct C), , , , (, ,....), .

( , JSON) /, . Jansson, JsonCPP ..

:

JSON journal_entry, , JSON,

{ "id": 1234,
  "title": "Some Title Here",
  "body": "Some body string goes here" }

, JSON journal_entry JSON ( ), / JSON

(, sqlite ..)


PS. ( - ) . , .

. . , Ocaml Marshal, Python pickle

+1

, , , , , . , ( , ):

typedef struct {
        int id;
        char title[MAX_TITLE_LEGNTH];
        char body[MAX_BODY_LENGTH];
} journal_entry;

malloc, "header", . , , , .

.. :

FILE* fp = fopen(<your-file-name>,"wb");
size_t size = sizeof(id)+strlen(title)+1+strlen(body)+1;
fwrite(&size, sizeof(size), 1, fp);
fwrite(&id, sizeof(id), 1, fp);
fwrite(title, sizeof(char), strlen(title)+1, fp);
fwrite(body, sizeof(char), strlen(body)+1, fp);
fclose(fp);

( , ):

FILE* fp = fopen(<your-file-name>,"rb");
size_t size;
int read_bytes = 0;
struct journal_entry je;
fread(&size, sizeof(size), 1, fp);
void* buf = malloc(size);
fread(buf, size, 1, fp);
fclose(fp);
je.id = *((int*)buf);  // might break if you wrote your file on OS with different endingness
read_bytes += sizeof(je.id)
je.title = (char*)(buf+read_bytes);
read_bytes +=  strlen(je.title)+1; 
je.body = (char*)(buf+read_bytes);
// other way would be to malloc je.title and je.body and destroy the buf
+1

. . - uint32_t, , . . .

0
source

Source: https://habr.com/ru/post/1620991/


All Articles