0

I have added the Fprintf into the SavePacket function but it doesn't recognise pos->destination like it does withing the outpackets function, How do i addapt the code so it takes the data saved in my Link-List and FprintF's it to my File?

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;

/*************************************** Save Pakcet Code function /***************************************

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);



}
4

1 回答 1

1

首先,您应该查看 和的函数签名。你进得很好。但是,这里是签名:printffprintfprintfoutputPacketsfprintf

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

第一个参数应该是 a 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);
于 2013-05-03T12:59:09.847 回答