使用带指针的结构读取二进制文件中的某个位置

时间:2018-06-10 16:15:07

标签: c file binaryfiles

我在二进制文件中存储这样的结构:

typedef struct user {
    char nick[6];
    int n_following;
    following *following;
}user;

它包含这种类型的结构数组:

typedef struct following{
    char nick[6];
    int last_message;
}following;

我想在二进制文件中创建和存储各种用户,每次我这样做时,我将它们的位置存储在哈希表中的文件中,以便以后能够使用fseek()来查找它们,这里是一个方面。哈希表中的项目:

typedef struct user_pos {
    char nick[6];
    int position_in_file;
}user_pos;

我试图实现上面的ideia并创建两个用户,然后尝试获取它们,但我只能得到一个,因为另一个打破了程序。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct following{
    char nick[6];
    int last_message;
}following;

typedef struct user {
    char nick[6];
    int n_following;
    following *following;
}user;    

void insert_user(char *input_a, int n){

    FILE *p;
    p=fopen("users","ab");
    user *new_user = malloc(sizeof(user));
    strcpy(new_user->nick, input_a);
    new_user->n_following = n;
    new_user->following = malloc(sizeof(following) * new_user->n_following);
    strcpy(new_user->following[0].nick, "la");
    new_user->following[0].last_message = 0;
    strcpy(new_user->following[1].nick, "zz");
    new_user->following[1].last_message = 2;

    fwrite(&new_user->nick, sizeof(new_user->nick), 1, p);
    fwrite(&new_user->n_following, sizeof(new_user->n_following), 1, p);
    fwrite(new_user->following, sizeof(following), new_user->n_following, p);
    printf("user created\n");
    fclose(p);
}

void get_user_info(char *input_a, int pos){


    FILE *p;
    p=fopen("users","rb");
    user *print_user = malloc(sizeof(user));

    fseek(p, pos* sizeof(user), SEEK_SET);

    fread(&print_user->nick, sizeof(print_user->nick), 1, p);
    fread(&print_user->n_following, sizeof(print_user->n_following), 1, p);
    print_user->following = malloc(sizeof(following));
    fread(print_user->following, sizeof(following), 2, p);

    printf("nick: %s, following: %d\n", print_user->nick, print_user->n_following);
    for(int i = 0; i < print_user->n_following; i++){
        printf("nick: %s, last_read: %d\n", print_user->following[i].nick, print_user->following[i].last_message);
    }

    fclose(p);
}

int main(){
    //hashtable *active_users = create();
    char buffer[38];
    char tipo;
    int n;
    char input_a[6];
    while(fgets(buffer, 38, stdin)){
        sscanf(buffer, "%c %s %d", &tipo, input_a, &n);
        switch(tipo) {
            case 'U' :
                insert_user(input_a, n);
                break;
            case 'S' :
                get_user_info(input_a, n);
                break;
            case 'X' :
                exit(0);
            default :
                printf("Operacao invalida\n");
        }
    }
    return 0;
}

程序因这种输入而失败:

input: U me 2
output: user created
input: U ro 2
output: user created
input: S me 0
output: nick: me, following: 2
        nick: la, last_read: 0
        nick: zz, last_read: 2
input: S ro 1//program breaks

为什么上面的程序找到了一个用户而不是另一个?

注意:我知道这对普通的.txt文件更好,但我想用fseek()函数的速度来获取文件中的用户

2 个答案:

答案 0 :(得分:2)

您无法致电fseek(p, pos* sizeof(user), SEEK_SET);跳至文件中的pos用户。 sizeof(user)返回user struct的字节大小,但此结构中有指针following,指向由following个对象填充的数组。如果您想跳到pos用户,您应该知道每个用户中有多少个跟踪对象。您没有这些信息,因此您可以逐个阅读文件用户,以便与pos用户联系。

// pseudocode 
get_user_info (int pos) {
   while (pos--) {
      skip 6 bytes // nick
      read 4 bytes n_following;
      skip n_following * sizeof(following) bytes
   }
   // here you can read data of pos user
}

答案 1 :(得分:1)

我在get_user_info函数中看到了三个问题。

  1. 偏移计算不包含following结构。该文件包含user结构,其中包含一些following结构,然后是下一个user结构。因此,如果没有阅读每个用户结构来获取n_following成员,就无法计算偏移量。

  2. 仅一个malloc结构的代码following空间,但随后将2个结构读入该内存。内存量应为sizeof(following) * print_user->n_following

  3. 在阅读以下结构时,fread使用的是硬代码2而不是n_following