C语言 FprintF从链接表到文件



我已经将Fprintf添加到SavePacket函数中,但它不识别pos->目的地,就像它在outpackets函数中所做的那样,我如何调整代码,以便将保存在我的链接列表和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 Listnn");
}

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;

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 保存包码功能/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

void SavePacket(){
FILE *inFile ;
char inFileName[10] = { '' } ;
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);

}

首先看一下printffprintf的函数签名。你的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是无用的,您可以删除它。

EDIT:确切地说,这一行应该是

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

相关内容

  • 没有找到相关文章

最新更新