I need to build a program that reads each record, and according to that record information would update some other records on the same file. For that, I was thinking in this approach:

```
int main(int argc, char *argv[]) {
    FILE *my_file;
    int files_read;
    struct my_struct an_struct;

    my_file = fopen("myfile.dat", "rb");

    files_read = fread(&an_struct, sizeof(struct my_struct), 1, my_file);
    printf("main->files_read: %d \n", files_read); //This prints one

    while (files_read == 1) {
        do_update();
        files_read = fread(&an_struct, sizeof(struct my_struct), 1, my_file);
        printf("main->files_read: %d \n", files_read); //This prints one
    }

    fclose(archivo_paises);
    return 0;
}

```

In the main function I'm reading the contents of the file, and every time I call `read` I get one as a response until I reach the end of the file. The problem is in the `do_update` function:

```
void do_update() {
    FILE *my_file;
    int files_read;
    struct my_struct an_struct;
    struct my_struct another_struct;

    my_files = fopen("myfile.dat", "wb+"); //Using rb+ solves it

    files_read = fread(&an_struct, sizeof(struct my_struct), 1, my_file);
    printf("do_update->files_read: %d \n", files_read); 
    //This printed zero!. Prints one using rb+

    while (files_read == 1) { //This never gets executed. Unless you use rb+

        if(something){
            fwrite(&another_struct, sizeof(struct my_struct), 1, my_file); 
            // Using rb+, this returns zero and didn't update
        }
        files_read = fread(&an_struct, sizeof(struct my_struct), 1, my_file);
        printf("do_update->files_read: %d \n", files_read);     
    }

    fclose(my_file);
}

```

What's happening is that the `files_read` variable gets the value of zero after the `read` call, so the logic to update the file is never executed.

Why is `read` returning zero when opening a file for `wb+`?

**Update:**
Using `rb+` as file mode on `do_update()` works, but now the call to `fwrite()` always returns zero, and it didn't update the file. Is is related to the mode?