2013-05-03 19 views
0

我已經將Fprintf添加到SavePacket函數中,但它不能識別pos->目標,就像它使用outpackets函數一樣,如何添加代碼以便它將數據保存在我的鏈接中列表和FprintF它到我的文件?FprintF從linklist到文件

void outputPackets(node **head) 
{ 


/********************************************************* 
* Copy Node pointer so as not to overwrite the pHead  * 
* pointer            * 
**********************************************************/ 
node *pos = *head; 

/********************************************************* 
* Walk the list by following the next pointer   * 
**********************************************************/ 
while(pos != NULL) { 
    printf("Source: %i Destination: %i Type: %i Port: %i \n", pos->Source, pos->Destination, pos->Type, pos->Port, pos->next); 
    pos = pos->next ; 
} 
printf("End of List\n\n"); 
} 



void push(node **head, node **aPacket) 
{ 
/********************************************************* 
* Add the cat to the head of the list (*aCat) allows the * 
* dereferencing of the pointer to a pointer    * 
**********************************************************/ 
(*aPacket)->next = *head; 
*head = *aPacket; 
} 

node *pop(node **head) 
{ 
/********************************************************* 
* Walk the link list to the last item keeping track of * 
* the previous. when you get to the end move the end  * 
* and spit out the last Cat in the list     * 
**********************************************************/ 
node *curr = *head; 
node *pos = NULL; 
if (curr == NULL) 
{ 
    return NULL; 
} else { 
    while (curr->next != NULL) 
    { 
     pos = curr; 
     curr = curr->next; 
    } 
    if (pos != NULL) // If there are more cats move the reference 
    { 
     pos->next = NULL; 
    } else {   // No Cats left then set the header to NULL (Empty list) 
     *head = NULL; 
    } 
} 
return curr; 

/* ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** 保存Pakcet代碼功能 / ** * ** * ** * ** * ** * ** * * * * ** * ** * ** * ** * **所有的* ** *

void SavePacket(){ 

FILE *inFile ; 
char inFileName[10] = { '\0' } ; 

printf("Input file name : ") ; 
scanf("%s", inFileName) ; 

unsigned long fileLen; 

//Open file 
inFile = fopen(inFileName, "w+"); 
if (!inFile) 
{ 
fprintf(stderr, "Unable to open file %s", &inFile); 
exit(0); 

} 

fprintf("Source: %i Destination: %i Type: %i Port: %i \n", pos->Source, pos->Destination, pos->Type, pos->Port, pos->next); 



} 

回答

1

首先,你應該看看printffprintfthe function signatures。你的printfoutputPackets很好。然而,這裏是fprintf簽名:

int fprintf(FILE* stream, const char* format, ...); 

第一個參數應該是一個FILE*。但是,你叫你的函數那樣:

fprintf("Source: %i Destination: %i Type: %i Port: %i \n", pos->Source, pos->Destination, pos->Type, pos->Port, pos->next); 

在你的電話,第一個參數是格式字符串,而應該是一個FILE*。這就是爲什麼你沒有得到你預期的結果。

此外,在撥打printffprintf的兩個電話中,您給出的最後一個值pos->next都是無用的,您可以將其刪除。

編輯:確切的說,這條線應該已經

fprintf(inFile, "Source: %i Destination: %i Type: %i Port: %i \n", pos->Source, pos->Destination, pos->Type, pos->Port); 
+0

好吧,我改變了代碼本, fprintf中INT((INFILE,爲const char * POS->源,POS->目標,pos-> Type,pos-> Port); 但我仍然收到一個錯誤,說'預期的聲明說明符或'......'之前)令牌' – user1949280 2013-05-03 13:35:22

+0

@ user1949280要小心,你把兩個''一開始,你在中間留下了一個流浪的'const char *',不管怎樣,我完成了我的答案。 – Morwenn 2013-05-03 13:36:17

+0

int fprintf(inFile,const char *; pos-> Source,pos-> Destination,pos-> Type,pos-> Port); 在const之前有一個錯誤sayin),但這會增加更多的問題? – user1949280 2013-05-03 13:38:56