2013-05-07 27 views
-1

我的fprintf()正在返回不可預知的結果。我認爲它是返回內存地址號而不是內存地址中的數據。有人可以看看我的代碼並檢查嗎?當我在fprintf()內使用&source時,我被告知它沒有聲明,當我在函數的頂部聲明它不起作用時。fprintf輸出正在返回不可預知的結果

#include <stdio.h> 
#include <stdlib.h> 

int MenuLoop = 0; 
int MaxPackets = 4; 
int currentPackets= 0; 
int menu; 

/********************************************************* 
* Node to represent a Cat which includes a link reference* 
* a link list of nodes with a pointer to a Cat Struct * 
* would be better but this is for illustartion only!  * 
**********************************************************/ 
struct Packet { 
int Source; 
int Destination; 
int Type; 
int Port; 
char *Data; 
struct Packet *next; // Link to next Cat 
}; 

typedef struct Packet node; // Removes the need to constantly refer to struct 

/********************************************************* 
* Stubs to fully declared functions below    * 
**********************************************************/ 
void outputPackets(node **head); 
void push(node **head, node **aPacket); 
node* pop(node **head); 
void AddPacket(); 
void AddPacket(); 
void SavePacket(); 
void ShowCurrent(); 
void ExitProgramme(); 


main() { 

do{ 

Menu(); 

} while(menu<4); 

} 


void AddPacket(){ 

int option; 

/********************************************************* 
* pointers for the link list and the temporary P to * 
* insert into the list         * 
**********************************************************/ 
node *pPacket, *pHead = NULL; 

/********************************************************* 
* Create a cat and also check the HEAP had room for it * 
**********************************************************/ 
pPacket = (node *)malloc(sizeof(node)); 
if (pPacket == NULL) 
{ 
    printf("Error: Out of Memory\n"); 
    exit(1); 
} 

currentPackets++; 
printf("Enter Source Number between 1-1024:\n"); 

scanf("%i", &pPacket->Source); 
printf("Enter Destination Number between 1-1024:\n"); 
scanf("%i", &pPacket->Destination); 
printf("Enter Type Number between 0-10:\n"); 
scanf("%i", &pPacket->Type); 
printf("Enter Port Number between 1-1024:\n"); 
scanf("%i", &pPacket->Port); 
printf("Enter Data Numberbetween 1-50:\n"); 
scanf("%s", &pPacket->Data); 
printf("Do you want to Enter another Packet?"); 
pPacket->next = NULL; 

/********************************************************* 
* Push the Cat onto the selected Link List, the function * 
* is written so the program will support multiple link * 
* list if additional 'pHead' pointers are created.  * 
* Who says you cannot herd cats!       * 
********************************************************** 
* NOTE: The push parameters are using references to the * 
* pointers to get round the pass by value problem caused * 
* by the way C handles parameters that need to be  * 
* modified            * 
**********************************************************/ 

push(&pHead, &pPacket); 

pPacket = (node *)malloc(sizeof(node)); 
if (pPacket == NULL) 
{ 
    printf("Error: Out of Memory\n"); 
    exit(1); 
} 

outputPackets(&pHead); 

/********************************************************* 
* Display the Link List 'pHead' is passed as a reference * 
**********************************************************/ 


return 0; 


do{ 
    if(currentPackets == MaxPackets); 
{ 
    printf("Packet limit reached please save\n"); 

} 


}while(currentPackets<MaxPackets); 

return 0; 
} 


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: %.4i Destination: %.4i Type: %.4i Port: %.4i \n", pos->Source, pos->Destination, pos->Type, pos->Port); 

    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; 

} 


void SavePacket(Source, Destination, Type, Port, Data){ 

FILE *inFile ; 
char inFileName[10] = { '\0' } ; 

printf("Input file name : ") ; 
scanf("%s", inFileName) ; 

//Open file 
inFile = fopen(inFileName, "w+"); 
if (!inFile) 
{ 
fprintf(stderr, "Unable to open file %s", &inFile); 
exit(0); 

} 

fprintf(inFile, "Source: %i Destination: %i Type: %i Port: %i Data: %s \n", Source, Destination, Type, Port, Data); 
fclose(inFile); 

} 


void ShowCurrent(){ 

} 

void ExitProgramme(){} 

void Menu(){ 

printf("********Welcome****** \n"); 
printf("Creator Ben Armstrong.\n\n"); 
printf("*Please Choose an option*\n"); 
printf("1. Add a new packet\n"); 
printf("2. Save current packet to file\n"); 
printf("3. Show current list of packets\n"); 
printf("4. Exit\n"); 

scanf("%i", &menu); 

switch(menu) 

{ 
    case 1: 
    AddPacket(); 
    break; 

    case 2: 
     SavePacket(); 
    break; 

    case 3 : 
     ShowCurrent(); 
    break; 

    case 4 : 
    ExitProgramme(); 
    break; 

} 


} 
+0

[你的確切代碼複製/粘貼到ideone.com給出14行錯誤](http://ideone.com/4rVmiH)。你可能想擺脫這些錯誤。 – pmg 2013-05-07 18:41:54

+0

你有什麼問題?您的SavePacket函數聲明甚至不是有效的。您在「無法打開文件」fprintf中以字符串形式打印地址...示例代碼中只有兩個fprintfs,並且只有一個使用源代碼,但由於您沒有在函數中指定變量類型宣言,我們不知道它在做什麼。 – xaxxon 2013-05-07 18:42:22

+1

這不算作SSCCE([Simple,Self-Contained,Complete Example](http://sscce.org/))。請努力將代碼降至最低。這將有助於突出顯示引起你問題的'fprintf()'調用 - 在格式化問題時我沒有注意到這種突出顯示。它還有助於正確縮進代碼(對於SO,空格而不是製表符,每個縮進級別有4個空格)。 – 2013-05-07 18:48:45

回答

2

這是你的問題的一部分...

inFile = fopen(inFileName, "w+"); 

if (!inFile) 
{ 
    fprintf(stderr, "Unable to open file %s", &inFile); 
    exit(0);  
} 

我敢肯定你的意思是......

inFile = fopen(inFileName, "w+"); 

if (!inFile) 
{ 
    fprintf(stderr, "Unable to open file %s", inFileName); 
    exit(0);  
} 

還要注意的是&inFile是​​變量的地址,而不是存儲在FILE指針中的值。

此外,聲明你的函數原型爲保存數據如下...

void SavePacket(Source, Destination, Type, Port, Data) 

它說你傳遞一個Source,一個Destination,一個Type,一個PortData的說法,但顯然你的意思是那些是名字,而不是類型,所以編譯器假設它們都是整數。你需要給他們一個類型和名稱...

void SavePacket(int Source, int Destination, int Type, int Port, char* Data) 

,現在你可以打印出來...

fprintf(
    inFile, "Source: %i Destination: %i Type: %i Port: %i Data: %s \n", 
    Source, Destination, Type, Port, Data 
); 

但是你有一個更大的爛攤子事情,因爲你聲明SavePacket()應該接收參數,但是當你把它叫做你沒有參數傳遞給它...

case 2: 
    SavePacket(); 
break; 

其中需要傳遞您需要打印的變量。喜歡的東西...

case 2: 
    SavePacket( 
     somePacket->Source, somePacket->Destination, somePacket->Type, 
     somePacket->Port, somePacket->Data 
    ); 
    break; 

但是,你可以更輕鬆地屏蔽這樣...

void SavePacket(struct Packet* packet) 
{ 
    ... 

    fprintf(
     inFile, "Source: %i Destination: %i Type: %i Port: %i Data: %s \n", 
     packet->Source, packet->Destination, packet->Type, 
     packet->Port, packet->Data 
    ); 
} 

然後通過傳入包叫它...

case 2: 
{ 
    struct Packet somePacket; 
    GetPacketDataFromSomewhere(&somePacket); 
    SavePacket(&somePacket); 
    break; 
} 
+0

感謝您的幫助,我已經使用上面的suggetion,我得到錯誤,無效的類型名稱'Packet at Void SavePacket(分組*包)。包*你的建議是否將結構傳遞給我的函數? – user1949280 2013-05-07 19:06:11

+0

是啊......對不起......我是在C++模式......應該是'結構數據包',而不是'數據包' - 相應地編輯。 – 2013-05-07 19:13:48

+0

我可以再問一件事,我在SavePacket(somePacket)中傳遞什麼;因爲我得到一個錯誤,如果我通過(結構數據包*數據包),抱歉的新問題即時通訊非常新的C編程 – user1949280 2013-05-07 20:05:21