1

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? (e.g.: 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!

3
  • 1
    what have you figured out so far? Commented Mar 6, 2013 at 9:01
  • 1
    Your example seems to be a text file, not a binary file ? Commented Mar 6, 2013 at 9:13
  • Why is this a binary file? Commented Mar 6, 2013 at 9:15

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

0

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);

4 Comments

Your code uses strtok and not strtok_r. Please also use strtok_r in your example.
@FUZxxl Apparently, it doesn't... :P
Either I can't see or you fixed it fast enough to not create a new version.
@FUZxxl I've fixed it within 5 minutes, and that doesn't go into the edit history.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.