FprintF从linklist到file

时间:2013-05-03 10:48:15

标签: c linked-list file-handling printf

我已将Fprintf添加到SavePacket函数中,但它不识别pos->目标,就像它使用outpackets函数一样,我如何添加代码,以便它保存在我的Link-List和FprintF中的数据它到我的文件?

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代码功能 / ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * < / p>

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 个答案:

答案 0 :(得分:1)

首先,您应该查看printffprintf的{​​{3}}。 printf outputPackets中的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*

在你的通话中,第一个参数是格式字符串,而它应该是printf。这就是为什么你没有得到你期望的结果。

此外,在您拨打fprintfpos->next时,您提供的最后一个值fprintf(inFile, "Source: %i Destination: %i Type: %i Port: %i \n", pos->Source, pos->Destination, pos->Type, pos->Port); 都没用,您可以将其删除。

编辑:确切地说,该行应该是

{{1}}