简体   繁体   中英

Reading non-fixed length from binary file (C)

I'm new here, and I need some help. :)

I am working on a program that has to write and read a binary file. I have to add lectures to it, they look like:

COURSECODE;COURSENAME;MAXAPPLICANTS;ACTUALAPPLICANTS;

I could write that in a file without any problems using char* .

My question is: how do I read that back in a struct if the records are non-fixed size? (eg: coursename can be Linear Algebra or Analysis -> length is non-determined) I also need to modify the actual applicants number, how do I find the character position of it, and the current line?

I'd be happy with ideas, and I would appreciate any source code as well, I was programming in C++ and C is a hard step-back for me.

Thank you in advance!

Your structure looks like

struct student {
char *coursecode;
char *coursename;
char *max_applicants;
char *actual_applicants;
};

Just add another member into your structure say int size which stores total size of structure.

Every time when you read from binary file you should read first 4 bytes you will get complete size of record,then see how many characters are there into record,read that much and tokenize string by ; you will find your records.

without termination characters it is impossible.

If you dedicate some character to split data apart, then its possible.

for instance, 3 strings can be told apart by their \\0. so you read until \\0, three times.

You could read the file into a char* buffer, then replace any ; with \\0 (the string termination character) and finally you take pointers of the begins of the fields into your struct:

struct student {
    char *coursecode;
    char *coursename;
    char *max_applicants;
    char *actual_applicants;
};

You might want to parse numeric fields with atoi first.

Piece of advice #1: if you're Hindi and you ever re-born, start by learning C, only then transition to C++.

Piece of advice #2: so if I understand correctly, you have four strings in a row, separated by semi-colons. Then you can use strtok_r() to split up each line and put the contents of the file in an array of structs (all error checking omitted for clarity, but you should definitely have some):

typedef struct {
    char *code;
    char *name;
    int max_appl;
    int cur_appl;
} Course;

char buf[1024];

FILE *f = fopen("courses.txt", "r");

size_t size = 0;
size_t allocsize = 8;
Course *c = malloc(allocsize * sizeof(*c));

char *end;

while (fgets(buf, sizeof(buf), f) != NULL) {
    if (size >= allocsize) {
        allocsize <<= 1;
        c = realloc(c, allocsize * sizeof(*c));
    }

    c[size].code = strdup(strtok_r(buf, ";", &end));
    c[size].name = strdup(strtok_r(NULL, ";", &end));
    c[size].max_appl = strtol(strtok_r(NULL, ";", &end), NULL, 10);
    c[size].cur_appl = strtol(strtok_r(NULL, "\n", &end), NULL, 10);

    size++;
}

int i;
for (i = 0; i < size; i++) {
    Course *p = c + i;
    printf("%s\n%s\n%d\n%d\n\n", p->code, p->name, p->max_appl, p->cur_appl);
    free(p->code);
    free(p->name);
}

free(c);
fclose(f);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM