[英]reading from a file using tokens to different structs
我有兩個不同的結構,其中following
是鏈表的一個節點
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
結構,並用“!”分隔內容 填充following
結構。
注意:文件每一行的“第二”元素將是用戶的名稱,該名稱最多25個字符,並且可以用空格分隔。 例如,“來自寒冷的人”是有效名稱。
我試圖這樣填充它們:
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種類型的令牌; [ (
; [ (
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.