[英]FprintF from linklist to file
我已經將Fprintf添加到SavePacket函數中,但是無法像使用outpackets函數那樣識別pos->目的地,如何添加代碼,以便將保存在我的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代碼功能/ ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** *
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);
}
首先,您應該查看printf
和fprintf
的函數簽名 。 您在outputPackets
中的printf
很好。 但是,這是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
和fprintf
調用中,您提供的最后一個值pos->next
是無用的,您可以刪除它。
編輯:確切地說,該行應該是
fprintf(inFile, "Source: %i Destination: %i Type: %i Port: %i \n", pos->Source, pos->Destination, pos->Type, pos->Port);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.