gpt4 book ai didi

c - 使用 token 从文件读取不同的结构

转载 作者:行者123 更新时间:2023-11-30 16:31:25 25 4
gpt4 key购买 nike

我有两个不同的结构,其中以下是链表的节点

typedef struct following{
char nick[6];
int last_message;
bool first_time;
struct following *next;
}following;

typedef struct user{
char nick[6];
char name[26];
int n_messages;
int n_following;
int n_followers;
following *arr_following;
following *arr_unfollowed;
}user;

我必须通过从这样的文件中读取来填充user结构:

zsq4r Pseu Donym 3 1 2;zero7 2 true!
zero7 James Bond 4 3 3;zsq4r 3 true!zero7 4 false!MrPym 1 true!
MrPym A Perfect Spy 1 3 1;zsq4r 3 true!zero7 4 true!AlecS 1 true!
AlecS He Who Came from the Cold 1 0 1;

由“;”分隔的内容就是填充user结构体以及以“!”分隔的内容填充以下结构。

注意:文件每行的“第二个”元素将是用户名,最多可包含 25 个字符,并可以用空格分隔。例如,“He Who Came from the Cold”是一个有效的名称。

我尝试像这样填充它们:

void read_from_file(hashtable *active_users, FILE *fp_active){
const char *delimiter1 = "!";
const char *delimiter2 = ";";
char *last_token;
char buffer[1540];
while(fgets(buffer, 1540, fp_active)) {
user *new_user = malloc(sizeof(user));
last_token = strtok( buffer, delimiter2);
while( last_token != NULL ){
sscanf(last_token,"%s %[^\n] %d %d %d", new_user->nick, new_user->name, &new_user->n_messages, &new_user->n_following,
&new_user->n_followers);
last_token = strtok( NULL, delimiter1);
}
insert(active_users, new_user);
}
}

尽管“last_token”变量保存了每次循环时从文件中读取的字符串的正确部分,但我找不到填充这两个结构的方法,因为 sscanf 仅填充了user 结构。

如有任何帮助,我们将不胜感激。

最佳答案

根据评论中的提示,我设法解决了我的问题,将文件的方面更改为:

zero7;James Bond;2;1;0[MrPym;1;true](zero7;0;false)
MrPym;A Perfect Spy;1;0;1
zsq4r;Pseu Donym;3;1;2[zero7;2;true]
zero7;James Bond;4;3;3[zsq4r;3;true][zero7;3;false][MrPym;1;true]
MrPym;A Perfect Spy;1;3;1[zsq4r;3;true][zero7;4;true][AlecS;1;true]
AlecS;He Who Came from the Cold;1;0;1

从此我使用以下代码将信息提取到不同的结构中:

void read_from_file(hashtable *active_users, hashtable *inactive_users, FILE *fp_active, FILE *fp_inactive){
char m_bool[6];
char *first_token;
char *last_token;
char buffer[1540];
char buffer2[1540];
while(fgets(buffer, 1540, fp_active)) {
strcpy(buffer2, buffer);
user *new_user = malloc(sizeof(user));
new_user->arr_following = NULL;
new_user->arr_unfollowed = NULL;
last_token = strtok( buffer, "[");
sscanf(last_token,"%[^;]; %[^;]; %d; %d; %d", new_user->nick, new_user->name, &new_user->n_messages, &new_user->n_following,
&new_user->n_followers);
last_token = strtok( NULL, "[");
while(last_token != NULL){
following *tmp_following = malloc(sizeof(following));
sscanf(last_token," %[^;]; %d; %5s", tmp_following->nick, &tmp_following->last_message, m_bool);
if(strcmp(m_bool, "true]") == 0)
add(&new_user->arr_following, tmp_following->nick, tmp_following->last_message, true);
else
add(&new_user->arr_following, tmp_following->nick, tmp_following->last_message, false);
last_token = strtok( NULL, "[");
}
first_token = strtok( buffer2, ")");
while(first_token != NULL && strcmp(first_token, buffer2) != 0){
following *tmp_following = malloc(sizeof(following));
sscanf(first_token," %[^;]; %d; %5s", tmp_following->nick, &tmp_following->last_message, m_bool);
if(strcmp(m_bool, "true]") == 0)
add(&new_user->arr_unfollowed, tmp_following->nick, tmp_following->last_message, true);
else
add(&new_user->arr_unfollowed, tmp_following->nick, tmp_following->last_message, false);
first_token = strtok( NULL, "(");
}
insert2(active_users, new_user);
}
}

我必须为每一行重新运行文件字符串 2 次,因为有 3 种类型的标记,; [(

关于c - 使用 token 从文件读取不同的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50629111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com