2013-05-03 64 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