我試圖實現一個保存系統,該系統允許我保存用戶在switch語句中輸入的「數據包」結構的用戶輸入數據。輸入的每條記錄都存儲在一個單獨的文件中,通過純文本行分隔。它還應該提示用戶他們想要命名文件,然後程序應該指出有多少記錄已經保存到文件中,最後如果沒有爲保存文件輸入名稱,它應該返回到主菜單。將數據結構保存到文本文件
從底部的void save函數中可以看到,我試圖實現這個保存系統,但是當我運行程序並選擇S將記錄保存到文件中時,它只是在輸入名稱之後崩潰文件名。所以如果有人能夠幫助我,那會很棒。
UPDATE
通過調試運行程序後,我得到了以下錯誤
UPDATE
固定竊聽錯誤,但文本文件,其中的數據將輸入文件名時,程序崩潰也可以保存。
#include <stdio.h> //library including standard input and output functions
#include <stdlib.h> //library including exit and system functions used below
#include <string.h> //library including string functions used
struct packet{
int source;
int destination;
int type; // Varibles for the structure
int port;
char data[50];
char * filename;
};
void save(int, struct packet*); //function to save the records stored to a file
int main()
{
struct packet s[50]; //Array for structure input
char choice;
int NetworkPacket = 0, ii = 0;
int recordCount = 0;
struct packet *records;
struct packet *temp;
records = malloc(sizeof(struct packet));
do{
system("cls");
puts("Please enter an option:\n");
puts("'A' Add a packet:\n");
puts("'D' to Display the Packet List:\n"); // The Menu system
puts("'S' to Save the Packets to a file:\n");
puts("'C' to Clear the list of current saved packets:\n");
puts(" or X to exit the program...\n");
//wait for user input
scanf("%c", &choice); //take the first char of user input and assing the value to
//the variable choice using the address of (&) the variable
if(choice == '\n') //check to see that the character is not \n left over from the
scanf("%c", &choice); //previous choice made, if it is then we need to scan the input again
switch (choice)
{
case 'A': system("cls"); //clear the screen
if(NetworkPacket==50)
{
printf("No more network packets can be added"); // if statement giving the limit of 50 packets allowed to be added
getch(); // User must press a key to continue
continue;
}
else{
printf("\n****Adding a packet*****\n");
printf("Where is the packet from?\n");
scanf("%i", &s[NetworkPacket].source);
printf("Where is the packet going?\n");
scanf("%i", &s[NetworkPacket].destination);
printf("What type is the packet?\n");
scanf("%i", &s[NetworkPacket].type); // collecting the data of the packet inputted by the user
printf("What is the packet's port?\n");
scanf("%i", &s[NetworkPacket].port);
printf("Enter up to 50 characters of data.\n");
scanf("%s", s[NetworkPacket].data);
NetworkPacket++;
}break;
case 'D': system("cls"); //clear the screen
printf("\nDisplaying Infomation\n");
if(NetworkPacket==0) // if statement stating to the user no records are shown if none are avalible
{
printf("no records yet, Please press any key to revert back to the main menu\n");
getch(); // User must press a key to continue
continue;
}
else{
for(ii = 0; ii < NetworkPacket; ii++)
{ // adds a 1 onto the NetworkPacket counter keeping a tally on the number of records stored.
printf("\nSource: %d", s[ii].source);
printf("\nDestination: %d", s[ii].destination);
printf("\nType : %d", s[ii].type);// if statement giving the limit of 50 packets allowed to be added
printf("\nPort : %d", s[ii].port);
printf("\nData: %s\n---\n", s[ii].data);
}getch();
} break;
case 'S':
system("cls"); //clear the screen
save(NetworkPacket, records); //S was chosen so use the Save function
break; //the while condition is True so break the switch and loop around
case 4: break;
case 5: break;
default:system("cls"); printf("%c is not an option\n Try again...\n", choice); // this message is shown if user doesn't input of one the case statment letters.
}
}while(choice!='X'); // exits the program
return 0;
}
void save(int rCount, struct packet *NetworkPacket){
FILE *recordFile; //file handle
char fileName[30] = { '\0'}; //string to store the file name
int i;
puts("Enter a filename to save the records :"); //ask the user for the filename
scanf("%s", fileName); //store the filename: data input should be checked
//here in your program
//try and open the file for writing and react accordingly if there is a problem
if((recordFile = fopen(fileName,"w"))==NULL){
printf("Couldn't open the file: %s\n",fileName);
exit(1);
}
else{ //the file opened so print the records array of packets to it
for(i=0;i<rCount;i++){
fprintf(recordFile,"%04d %04d %04d %04d %s\n",
NetworkPacket[i].source,
NetworkPacket[i].destination,
NetworkPacket[i].type,
NetworkPacket[i].port,
NetworkPacket[i].port,
NetworkPacket[i].data);
}
fclose(recordFile); //close the file
}
}
請格式化您的代碼,這很難閱讀。你嘗試使用調試器嗎? –
是的,我已經試過調試器,它聲明 第5行:警告:「結構數據包」內部參數列表 –
聲明我也刪除了休息,謝謝你,但很明顯,我仍然有這個問題 –