2014-07-21 98 views
-1

我需要使用fgets讀入文件的函數的幫助。我們已經給出了該功能應該做什麼的描述,所以我沒有做到這一點,但它仍然無法正常工作。我在這裏先向您的幫助表示感謝。使用fgets在文件中讀取

typedef struct StringArray_ { 
    char **strings; 
    int size; 
}StringArray; 

void create_commands_tree(Command **commands, const char *file); 
void insert_into_commands_tree(Command** node, char** data); 
StringArray* tokenizer (char *string, const char* delimiters); 

void create_commands_tree(Command **commands, const char *file) { 

    FILE *input; 
    input = fopen(file, "r"); 
    char strings[256]; 
    StringArray *temp; 

    while(fgets(strings,100,input)){ 

      temp = tokenizer(strings, "\n"); 
    } 
      insert_into_commands_tree(temp, temp->strings); 
} 

功能的描述是這樣的:

Reads from a file the game commands the checker uses instead of using fscanf you  
are required to use fgets to read in an line of characters(a string including the  
newline character and the adds the null terminator) into a local buffer. That local  
buffer will be passed to our tokenizer function that will split the string passed into an  
array of strings based off our passed in delimiters. The delimiters that we are using  
are 「,\n」. Pass the returned StringArray structure’s field strings to the  
insert_into_commands_tree with the commands double pointer.  

,我要讀的文件是

pickup,2 
help,1 
quit,1 
load,2 
+0

你還沒有說過什麼問題,甚至問了一個具體的問題? –

+0

我是否正確地使用fgets? –

+0

當然 - 但是是什麼讓你覺得'fgets'是問題所在? –

回答

1

閱讀每一行後,您需要將它傳遞給void insert_into_commands_tree(Command** node, char** data);否則,在你的代碼中,它只會傳遞文件的最後一行

Make this c絞殺並嘗試它 -

while(fgets(strings,100,input)){ 

    temp = tokenizer(strings, "\n"); 
    insert_into_commands_tree(temp, temp->strings); 
} 
相關問題