airport Log program using 3D linked List : problem reading from file

This is a discussion on airport Log program using 3D linked List : problem reading from file within the C Programming forums, part of the General Programming Boards category; Hi ! This is a program i wrote which creates a 3D linked list. A short brefing of the program ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    91

    airport Log program using 3D linked List : problem reading from file

    Hi !

    This is a program i wrote which creates a 3D linked list. A short brefing of the program is as follows:

    1. creates a linked list of all the airlines on the airport
    2. Each airline node holds another node to generate a linked list of all the flights for that airline
    3. Each flight node holds another node to generate a linked list of all the passengers travelling on the flight.

    The program works fine without using files.

    I am trying to implement this program using files and have ran into problems I cannot understand:

    The read function fails to read properly and generate a proper 3D list. The only sucessfull list generated is the airline name list and it fails.

    Failures include things like:

    1. running into infinite loop when displaying a flight list for an airline
    2. running into infinite loop when displaying passengers for a flight
    3. if the first airline name has no flights and the second airline name has flights then the read function links the flights of second airline to the first airline also

    Iam still testing more on this program and will keep trying to solve more bugs in it plus also try to solve the ones above

    Here is quick breifing on how the files generated and read are structured : (All thanks to hk_mp5kpdw for this )

    //write data to file before exiting the program
    void write_to_file(airline *start1)

    The structure of the written file is like this :

    Code:
    [number of airlines]
    [first airline's name]
        [number of flights for first airline]
        [first airline's, first flight's information]
        [first airline's, first flight's number of passengers]
            [first airline's, first flight's, first passengers information]
            [first airline's, first flight's, next passengers information]
            [etc...]
        [first airline's, second flight's information]
        [first airline's, second flight's number of passengers]
            [first airline's, second flight's, first passenger's information]
            [first airline's, second flight's, next passenger's information]
            [etc...]
        [etc...]
    [second airline's name]
        [etc...]
    Now when i read the file this is the structure of the program i use:

    start1 read_from_file();

    Code:
    FILE* fp;
    struct airline *temp;
    int count = 0;
    
    // Open file for writing
    fp = fopen("airline.txt","wb");  // Note binary mode
    
    // Determine number of airlines and write to file
    for( temp = start1; temp != NULL; temp = temp->next ) ++count;
    fwrite(&count,sizeof(int),1,fp);
    
    // Loop through and write airlines data to file
    for( temp = start1; temp != NULL; temp = temp->next )
    {
        // Determine length of airline name and write to file
        int length = strlen(temp->name);
        fwrite(&length,sizeof(int),1,fp);
    
        // Write airline name to file
        fwrite(temp->name,length,1,fp);
    
        // Write airline flight information to file
        determine and write number of flights for current airline to file
        for( loop through flights for current airline )
        {
            // Write passenger data to file
            determine and write number of passengers for current flight to file
            for( loop through passengers in current flight )
            {
            }
        }
    }
    fclose(fp);

    and finally here is the enitre code, I hope is not too big to post :

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
     
    struct flight;                               //Forward Declaration of Flight structure
    
    
    //-----------------------------------   AIRLINE LIST STRUCTURE AND FUNCTION DECLARATIONS ----------------------------------------
    
    
    
    struct airline                          // Structure for Airline node, includes strings for Airline name 
    {                                      // pointer locations to the next airline and also to the flight list for that airline         
    	char *airlinename;
    	struct airline *next;
    	struct flight *node1;
    } *start1 = NULL;
    
    
    airline* create_airline_node(airline *start1);             //Function Declarations
    airline* enter_airline_info(airline *node);
    airline* delete_airline_node(airline *start1);
    void print_airline_list(airline *start1);
    airline* enter_flight_menu(airline *start1);
    void write_to_file(airline *start1);
    airline * read_from_file();
    airline* airline_menu(airline *start1);
    
    
    
    
    //-----------------------------------   FLIGHT LIST STRUCTURE AND FUNCTION DECLARATIONS ----------------------------------------
    
    struct passenger;                              //Forward Declaration of passenger structure
    
    
    struct flight                          // Structure for flight node, includes strings for flightname, from, to and time with 
    {                                      // pointer locations to the next flight and also to the passenger list          
    	char *flightname;
    	char *departure_location;
    	char *arrival_location;
    	char *take_off_time;
    	struct flight *next;
    	struct passenger *node2;
    } *start2 = NULL;
    
    
    flight* create_flight_node(flight *start2);             //Function Declarations
    flight* enter_flight_info(flight *node);
    flight* delete_flight_node(flight *start2);
    void print_flight_list(flight *start2);
    flight* enter_passenger_menu(flight *start2);
    flight* flight_menu(flight *start2);
    
    
    
    //-----------------------------------   PASSENGER LIST STRUCTURE AND FUNCTION DECLARATIONS ----------------------------------------
    
    
    struct passenger                                 //Passenger Structure includes name of the passenger and the ponter to the next
    {                                                // passenger node, structure declaration also declares start as the first node
    	char *name;                                  // and declares it NULL.
    	struct passenger *next;
    } *start3 = NULL;
    
    
    
    passenger* create_passenger_node(passenger *start3);             //Function Declarations
    passenger* enter_passenger_info(passenger *node);
    passenger* delete_passenger_node(passenger *start3);
    passenger* passenger_menu(passenger *start3);
    void print_passenger_list(passenger *node);
     
    
    
    
    
    //----------------------------- MEMBER FUNCTIONS TO MANIPULATE THE AIRLINE LINKED LIST --------------------------------------------
    
    
    airline* create_airline_node(airline *start1)         // Creates a sorted list of airlines (default entry sort condition by airline name)
    {                                                   // by entering the airline name in a node at the sorted position, 
                                                        // it calls the function "enter_airline_info() to ask the 
    	if (start1 == NULL)                             // user for its information
    	{
    		start1 = (airline *) malloc(sizeof(airline));
    		start1 = enter_airline_info(start1);
    		start1->next = NULL;
    	}
    	else
    	{
    		airline *count_node1, *count_node2, *node;
    		node = (airline *) malloc(sizeof(airline));
    		node = enter_airline_info(node);
    		for (count_node1 = start1; (count_node1 != NULL) && (strcmp(count_node1->airlinename, node->airlinename) <= 0); count_node2 = count_node1, count_node1 = count_node1->next);
    		if(count_node1 == start1)
    		{
    			node->next = start1;
    			start1 = node;
    		}
    		else if (count_node1 == NULL)
    		{
    			count_node2->next = node;
    			node->next = NULL;
    		}
    		else
    		{
    			count_node2->next = node;
    			node->next = count_node1;
    		}
    	}
    	return start1;
    }
    
    
    airline* enter_airline_info(airline *node)             // Function to enter the information about the airline, this function is
    {                                                      // called from the function create_airline_node(airline *start1)by itself.   
    	printf("\n");                                      
    	printf(" Welcome to IGI International Airport\n");
    	printf("Please enter the Airline Name\n");
    	printf("\n");
    	fflush(stdin);
    	node->airlinename = new char [80];
    	gets(node->airlinename);
    	node->node1 = NULL;
    	return node;
    }
    
    airline* delete_airline_node(airline *start)          // Function to delete the name of a airline, function 
    {                                                    // searches for the name and deletes if it is found.
    	if(start1 == NULL)
    	{
    		printf("\n");
    		printf("Sorry, this Airport is not operational at this moment\n");
    	}
    	else
    	{
    		char airlinename[80];
    		printf("\n");
    		printf("Please enter the  name of the Airline you want to delete\n");
    		fflush(stdin);
    		gets(airlinename);
    		airline *count_node1, *count_node2;
    		airline *temp;
    		for(count_node1 = start1; (count_node1 != NULL) && (strcmp(count_node1->airlinename, airlinename) != 0); count_node2 = count_node1, count_node1 = count_node1->next);
    		if(count_node1!= NULL)
    		{
    			if (count_node1 == start1)
    			{
    				temp = start1;
    				start1 = start1->next;
    				free(temp);
    			}
    			else if(count_node1->next == NULL && count_node1 != start1) 
    			{
    				temp = count_node1;
    				count_node2->next = NULL;
    				free (temp);
    			}
    			else
    			{
    				temp = count_node1;
    				count_node2->next = count_node1->next;
    				free (temp);
    			}
    		}
    		else 
    		{
    			printf("\n");
    			printf("Sorry, this airline is currently not in service at this airport\n");
    		} 
    	}
    	return start1;
    }
    
    
    void print_airline_list(airline *start)                       //Function to print all the airlines operating on the airport
    {
    	if (start1 == NULL)
    	{
    		printf("\n");
    		printf("Sorry, this Airport is not operational at this moment\n");
    	}
    	else
    	{
    		printf("\n");
    		airline *count;
    		for (count = start1; count != NULL; count = count->next)
    		printf("%s\n", count->airlinename);
    	}
    }
    
    airline* enter_flight_menu(airline *start1)        // Function to search for the airline entered and enter its flight list menu
    {
    	char airlinename[80];
    	printf("\n");
    	printf("Welcome to the Flight list Menu\n");
    	printf("Enter the Airline name you wish to check\n");
    	fflush(stdin);
    	gets(airlinename);
    	airline *count;
    	for(count = start1; (count != NULL) && (strcmp(count->airlinename, airlinename) != 0); count = count->next);
    	if(count == NULL)
    	{
    		printf("\n");
    		printf("Sorry, this airline does not operate in this airport\n");
    		return start1;
    	}
    	else
    	{
    		printf("\n");
    		count->node1 = flight_menu(count->node1);
    		return start1;
    	}
    }
    
    
    void write_to_file(airline *start1)
    {
    	FILE *fp;
    	airline *count1;
    	flight *count2;
    	passenger *count3;
    	int length = 0;
    	int airline_count = 0;
    	int passenger_count = 0;
    	int flight_count = 0;
    	fp = fopen("airline.txt","wb");
    
    	// Determine number of airlines and write to file
    	for( count1 = start1; count1 != NULL; count1 = count1->next) ++airline_count;
    	fwrite(&airline_count,sizeof(int),1,fp);
    
    
    	// Loop through and write airlines data to file
    	for( count1 = start1; count1 != NULL; count1 = count1->next)
    	{
    		// Determine length of airline name and write to file
    		length = strlen(count1->airlinename);
    		fwrite(&length,sizeof(int),1,fp);
    
    		// Write airline name to file
    		fwrite(count1->airlinename,length,1,fp);
    
    		// Determine number of flights for the airline and write to file
    		for( count2 = count1->node1; count2 != NULL; count2 = count2->next) ++flight_count;
    		fwrite(&flight_count,sizeof(int),1,fp);
    
    		// Loop through and write flight data for the airline to file
    		for( count2 = count1->node1; count2 != NULL; count2 = count2->next)
    		{
    			// Determine length of flight name and write to file
    			length = strlen(count2->flightname);
    			fwrite(&length,sizeof(int),1,fp);
    
    			// Write flight name to file
    			fwrite(count2->flightname,length,1,fp);
    
    			// Determine length of departure location and write to file
    			length = strlen(count2->departure_location);
    			fwrite(&length,sizeof(int),1,fp);
    
    			// Write departure location to file
    			fwrite(count2->departure_location,length,1,fp);
    
    			// Determine length of arrival location and write to file
    			length = strlen(count2->arrival_location);
    			fwrite(&length,sizeof(int),1,fp);
    
    			// Write departure location to file
    			fwrite(count2->arrival_location,length,1,fp);
    
    			// Determine length of take off time and write to file
    			length = strlen(count2->take_off_time);
    			fwrite(&length,sizeof(int),1,fp);
    
    			// Write departure location to file
    			fwrite(count2->take_off_time,length,1,fp);
    
    
    			// Determine number of passengers for this flight and write to file
    			for( count3 = count2->node2; count3 != NULL; count3 = count3->next) ++passenger_count;
    			fwrite(&passenger_count,sizeof(int),1,fp);
    
    			// Loop through and write passenger data for the flight to file
    			for( count3 = count2->node2; count3 != NULL; count3 = count3->next)
    			{
    				// Determine length of passenger name and write to file
    				length = strlen(count3->name);
    				fwrite(&length,sizeof(int),1,fp);
    
    				// Write passenger name to file
    				fwrite(count3->name,length,1,fp);
    			}
    
    
    		}
    
    
    	}
    	fclose(fp);
    
    }
    
    
    airline * read_from_file()
    {
    	FILE *fp;
    	airline *start, *curr, *temp;
    	int length = 0;
    	int airline_count= 0;
    	int flight_count = 0;
    	int passenger_count = 0;
    
    	// Initialize pointers
    	start = curr = NULL;
    
    	// Open file for reading
    	fp = fopen("airline.txt","rb");
    
    	while(!feof(fp))
    	{
    		// Read in number of airlines stored in file
    		if( fp == NULL)
    			airline_count = 0;
    		else
    			fread(&airline_count,sizeof(int),1,fp);
    
    		// Loop through "airline_count" airlines
    		for( ; airline_count > 0; --airline_count )
    		{
    			// allocate room for a single airline structure and flight link to NULL
    			temp = (airline *)malloc(sizeof(airline));
    			temp->next = NULL;
    			temp->node1 = NULL;
    
    			// read airline name length
    			fread(&length,sizeof(int),1,fp);
    
    			//allocate sufficent memory for the airline name
    			temp->airlinename = (char *)malloc(length+1);
    
    			// read in airline's name and NULL terminate
    			fread(temp->airlinename,length,1,fp);
    			temp->airlinename[length] = 0;
    
    			// Add current airline to end of growing list of airlines
    			if( start == NULL ) 
    			{
    				start = curr = temp;
    			}
    			else
    			{
    				curr->next = temp;
    				curr = curr->next;
    			}
    
    			// Read in number of flights stored for the airline in file
    			fread(&flight_count,sizeof(int),1,fp);
    
    			// Loop through "flight_count" flight
    			for( ; flight_count > 0; --flight_count )
    			{
    				// allocate room for a single flight structure and set passenger link to NULL
    				curr->node1 = (flight *)malloc(sizeof(flight));
    				curr->node1->next = NULL;
    				curr->node1->node2 = NULL;
    
    				// read flight name length
    				fread(&length,sizeof(int),1,fp);
    
    				//allocate sufficent memory for the flight name
    				curr->node1->flightname = (char *)malloc(length+1);
    
    				// read in flight name and NULL terminate
    				fread(curr->node1->flightname,length,1,fp);
    				curr->node1->flightname[length] = 0;
    
    				// read departure location length
    				fread(&length,sizeof(int),1,fp);
    
    				//allocate sufficent memory for the departure location
    				curr->node1->departure_location = (char *)malloc(length+1);
    
    				// read in departure location and NULL terminate
    				fread(curr->node1->departure_location,length,1,fp);
    				curr->node1->departure_location[length] = 0;
    
    				// read arrival location length
    				fread(&length,sizeof(int),1,fp);
    
    				//allocate sufficent memory for the arrival location
    				curr->node1->arrival_location = (char *)malloc(length+1);
    
    				// read in arrival location and NULL terminate
    				fread(curr->node1->arrival_location,length,1,fp);
    				curr->node1->arrival_location[length] = 0;
    
    				// read take off time length
    				fread(&length,sizeof(int),1,fp);
    
    				//allocate sufficent memory for the take off time
    				curr->node1->take_off_time = (char *)malloc(length+1);
    
    				// read in take off time and NULL terminate
    				fread(curr->node1->take_off_time,length,1,fp);
    				curr->node1->take_off_time[length] = 0;
    
    				// Add current flight to end of growing list of flights
    				if( start->node1 == NULL ) 
    				{
    					start->node1 = curr->node1 = temp->node1;
    				}
    				else
    				{
    					curr->node1->next = temp->node1;
    					curr->node1 = curr->node1->next;
    				}
    
    			
    				// Read in number of passengers stored for that file
    				fread(&passenger_count,sizeof(int),1,fp);
    
    				// Loop through "passenger_count" passengers for this flight
    				for( ; passenger_count > 0; --passenger_count )
    				{
    					// allocate room for a single passenger structure
    					curr->node1->node2 = (passenger *)malloc(sizeof(passenger));
    					curr->node1->node2->next = NULL;
    
    					// read passenger name length
    					fread(&length,sizeof(int),1,fp);
    
    					//allocate sufficent memory for the passenger name
    					curr->node1->node2->name = (char *)malloc(length+1);
    
    					// read in passenger name and NULL terminate
    					fread(curr->node1->node2->name,length,1,fp);
    					curr->node1->node2->name[length] = 0;
    
    
    					// Add current passenger to end of growing list of passengers for this flight
    					if( start->node1->node2 == NULL ) 
    					{
    						start->node1->node2 = curr->node1->node2 = temp->node1->node2;
    					}
    					else
    					{
    						curr->node1->node2->next = temp->node1->node2;
    						curr->node1->node2 = curr->node1->node2->next;
    					}
    				}
    
    			}
    		}
    	}
    	fclose(fp);
    	return start;
    
    }
    
    airline* airline_menu(airline *start)                            // Menu Function which is handles the airlinelist
    {                                                                      
    	int choice;
    	printf("Welcome to the Main Menu\n");
    	printf("Please enter your choice from the menu shown below\n");
    	printf("1: Add a new airline to this airport\n");
    	printf("2: Delete an airline from the airport\n");
    	printf("3: Display the Airlines List at this Airport\n");
        printf("4: Enter Flight menu for an airline\n");
    	printf("5: Exit the program\n");
    	scanf ("%d", &choice);
    	switch(choice)
    	{
    	case 1: 
    		{ 
    			start1 = create_airline_node(start1);
    		} break;
    	case 2:
    		{  
    			start1 = delete_airline_node(start1);
    		} break;
    	case 3:
    		{
    			print_airline_list(start1);
    		} break;
    	case 4:
    		{
    			start1 = enter_flight_menu(start1);
    		} break;
    	case 5:
    		{
    			write_to_file(start1);
    			exit(1);
    		} break;
    	default:
    		{
    			start1 = airline_menu(start1);
    		} break;
    	}
    	printf("\n");
    	start1 = airline_menu(start1);
    	return start1;
    }
    
    
    //----------------------------- MEMBER FUNCTIONS TO MANIPULATE THE FLIGHT LINKED LIST --------------------------------------------
    
    
    flight* create_flight_node(flight *start2)           // Creates a sorted list of flights (default entry sort condition by flight no.)
    {                                                    // by entering the flight node at the sorted position, 
        printf("\n");                                    // it calls the function "enter_flight_info() to ask the 
    	if (start2 == NULL)                              // user for its information
    	{
    		start2 = (flight *) malloc(sizeof(flight));
    		start2 = enter_flight_info(start2);
    		start2->next = NULL;
    	}
    	else
    	{
    		flight *count_node1, *count_node2, *node;
    		node = (flight *) malloc(sizeof(flight));
    		node = enter_flight_info(node);
    		for (count_node1 = start2; (count_node1 != NULL) && (strcmp(count_node1->flightname, node->flightname) <= 0); count_node2 = count_node1, count_node1 = count_node1->next);
    		if(count_node1 == start2)
    		{
    			node->next = start2;
    			start2 = node;
    		}
    		else if (count_node1 == NULL)
    		{
    			count_node2->next = node;
    			node->next = NULL;
    		}
    		else
    		{
    			count_node2->next = node;
    			node->next = count_node1;
    		}
    	}
    	return start2;
    }
    
    
    flight* enter_flight_info(flight *node)               // Function to enter the information about the flight, this function is
    {                                                    // called from the function create_flight_node(flight *start)by itself.   
    	printf(" Welcome to the Flight List\n");
    	printf("Please enter the Flight Name\n");
    	fflush(stdin);
    	node->flightname = new char [80];
    	gets(node->flightname);
    	printf("Please enter the Departure Location\n");
    	fflush(stdin);
    	node->departure_location = new char [80];
    	gets(node->departure_location);
    	printf("Please enter the Arrival Location\n");
    	fflush(stdin);
    	node->arrival_location = new char [80];
    	gets(node->arrival_location);
    	printf("Please enter the Take off time\n");
    	fflush(stdin);
    	node->take_off_time = new char [80];
    	gets(node->take_off_time);
    	node->node2 = NULL;
    	return node;
    }
    
    flight* delete_flight_node(flight *start2)            // Function to delete the name of a flight, function 
    {                                                     // searches for the name and deletes if it is found.
    	printf("\n");
    	if(start2 == NULL)
    	{
    		printf("Sorry, no available Flights today\n");
    	}
    	else
    	{
    		char flightname[80];
    		printf("Please enter the flight name you want to delete\n");
    		fflush(stdin);
    		gets(flightname);
    		flight *count_node1, *count_node2;
    		flight *temp;
    		for(count_node1 = start2; (count_node1 != NULL) && (strcmp(count_node1->flightname, flightname) != 0); count_node2 = count_node1, count_node1 = count_node1->next);
    		if(count_node1!= NULL)
    		{
    			if (count_node1 == start2)
    			{
    				temp = start2;
    				start2 = start2->next;
    				free(temp);
    			}
    			else if(count_node1->next == NULL && count_node1 != start2) 
    			{
    				temp = count_node1;
    				count_node2->next = NULL;
    				free (temp);
    			}
    			else
    			{
    				temp = count_node1;
    				count_node2->next = count_node1->next;
    				free (temp);
    			}
    		}
    		else 
    		{
    			printf("\n");
    			printf("Sorry, this name was not found in this list\n");
    		} 
    	}
    	return start2;
    }
    
    
    void print_flight_list(flight *start2)
    {
    	printf("\n");
    	if (start2 == NULL)
    	{
    		printf("Sorry, the Flight List is empty\n");
    	}
    	else
    	{
    		flight *count;
    		for (count = start2; count != NULL; count = count->next)
    		{
    			printf("%s\n", count->flightname);
    		    printf("From %s\n", count->departure_location);
    		    printf("To %s\n", count->arrival_location);
    		    printf("Take off Time : %s\n", count->take_off_time);
    			printf("\n");
    		}
    	}
    }
    
    flight* enter_passenger_menu(flight *start2)        // Function to search for the flight entered and enter its passenger list menu
    {
    	printf("\n");
    	char flightname[80];
    	printf("Welcome to the passenger list Menu\n");
    	printf("Enter the Flight name you wish to check\n");
    	fflush(stdin);
    	gets(flightname);
    	flight *count;
    	for(count = start2; (count != NULL) && (strcmp(count->flightname, flightname) != 0); count = count->next);
    	if(count == NULL)
    	{
    		printf("Sorry, this flight is not available\n");
    		return start2;
    	}
    	else
    	{
    		count->node2 = passenger_menu(count->node2);
    		return start2;
    	}
    }
    
    flight* flight_menu(flight *start2)                          // Menu Function which is handles the flightlist for an airline
    {                                                                      
    	int choice;
    	printf("Welcome to the Flight menu\n");
    	printf("Please enter your choice from the menu shown below\n");
    	printf("1: Add a new Flight to this Airline\n");
    	printf("2: Delete a Flight from the Airline\n");
    	printf("3: Display the Flight List for the Airline\n");
        printf("4: Enter passenger menu for a flight\n");
    	printf("5: Return back to the Main Menu\n");
    	scanf ("%d", &choice);
    	switch(choice)
    	{
    	case 1: 
    		{ 
    			start2 = create_flight_node(start2);
    		} break;
    	case 2:
    		{  
    			start2 = delete_flight_node(start2);
    		} break;
    	case 3:
    		{
    			print_flight_list(start2);
    		} break;
    	case 4:
    		{
    			start2 = enter_passenger_menu(start2);
    		} break;
    	case 5:
    		{
    			return start2;
    		} break;
    	default:
    		{
    			start2 = flight_menu(start2);
    		} break;
    	}
    	printf("\n");
    	start2 = flight_menu(start2);
    	return start2;
    }
    
    
    //----------------------------- MEMBER FUNCTIONS TO MANIPULATE THE  PASSENGER LINKED LIST --------------------------------------------
    
    
    
    
    passenger* create_passenger_node(passenger *start3)  //Creates a sorted list of passengers by entering the passenger node at the
    {                                                    // sorted position, it calls the function "enter_passenger_info() to
        printf("\n");                                    // ask the user for its information
    	if (start3 == NULL)                              
    	{
    		start3 = (passenger *) malloc(sizeof(passenger));
    		start3 = enter_passenger_info(start3);
    		start3->next = NULL;
    	}
    	else
    	{
    		passenger *count_node1, *count_node2, *node;
    		node = (passenger *) malloc(sizeof(passenger));
    		node = enter_passenger_info(node);
    		for (count_node1 = start3; (count_node1 != NULL) && (strcmp(count_node1->name, node->name) <= 0); count_node2 = count_node1, count_node1 = count_node1->next);
    		if(count_node1 == start3)
    		{
    			node->next = start3;
    			start3 = node;
    		}
    		else if (count_node1 == NULL)
    		{
    			count_node2->next = node;
    			node->next = NULL;
    		}
    		else
    		{
    			count_node2->next = node;
    			node->next = count_node1;
    		}
    	}
    	return start3;
    }
    
    
    passenger* enter_passenger_info(passenger *node)     // Function to enter the name of the passenger, this function is called 
    {                                                    // from the function create_passenger_node(passenger *start)by itself.   
    	printf(" Welcome to the passenger list\n");
    	printf("Please enter your name\n");
    	fflush(stdin);
    	node->name = new char [80];
    	gets(node->name);
    	return node;
    }
    
    passenger* delete_passenger_node(passenger *start3)   // Function to delete the name of a passenger in the flight, function 
    {                                                     // searches for the name and deletes if it is found.
    	printf("\n");
    	if(start3 == NULL)
    	{
    		printf("\n");
    		printf("Sorry, the passenger list is empty\n");
    	}
    	else
    	{
    		char name[80];
    		printf("Please enter the name you want to delete\n");
    		fflush(stdin);
    		gets(name);
    		passenger *count_node1, *count_node2;
    		passenger *temp;
    		for(count_node1 = start3; (count_node1 != NULL) && (strcmp(count_node1->name, name) != 0); count_node2 = count_node1, count_node1 = count_node1->next);
    		if(count_node1!= NULL)
    		{
    			if (count_node1 == start3)
    			{
    				temp = start3;
    				start3 = start3->next;
    				free(temp);
    			}
    			else if(count_node1->next == NULL && count_node1 != start3) 
    			{
    				temp = count_node1;
    				count_node2->next = NULL;
    				free (temp);
    			}
    			else
    			{
    				temp = count_node1;
    				count_node2->next = count_node1->next;
    				free (temp);
    			}
    		}
    		else 
    		{
    			printf("\n");
    			printf("Sorry, this name was not found in this list\n");
    		} 
    	}
    	return start3;
    }
    
    passenger* passenger_menu(passenger *start3)                   // Menu Function which is called from main and takes 
    {                                                              // over the program list after that              
    	printf("\n");                                                     
    	int choice;
    	printf("Welcome to the passenger menu\n");
    	printf("Please enter your choice from the menu shown below\n");
    	printf("1: Add a passenger to this flight\n");
    	printf("2: Delete a passenger from this flight\n");
    	printf("3: Display the passenger list for this flight\n");
    	printf("4: Return back to the Flight Menu\n");
    	scanf ("%d", &choice);
    	switch(choice)
    	{
    	case 1: 
    		{ 
    			start3 = create_passenger_node(start3);
    		} break;
    	case 2:
    		{  
    			start3 = delete_passenger_node(start3);
    		} break;
    	case 3:
    		{
    			print_passenger_list(start3);
    		} break;
    	case 4:
    		{
    			return start3;
    		} break;
    	default:
    		{
    			start3 = passenger_menu(start3);
    		} break;
    	}
    	start3 = passenger_menu(start3);
    	return start3;
    }
    
    
    
    void print_passenger_list(passenger *start3)
    {
    	printf("\n");
    	if (start3 == NULL)
    	{
    		printf("Sorry, the passenger list is empty\n");
    	}
    	else
    	{
    		passenger *count;
    		for (count = start3; count != NULL; count = count->next)
    		printf("%s\n", count->name);
    	}
    }
    
    //--------------------------------- MAIN FUNCTION CALLING THE MENU FUNCTION ------------------------------------------------
    
    void main()
    {
    	start1 = read_from_file();
    	start1 = airline_menu(start1);
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    I copied your code into a blank project and compiled... no errors... that's good. I ran it and the program immediately crashed... not good.

    I put in some debug statements sprinkled here and there in the main and read_from_file functions. That would be a handy skill for you to develop by the way. I discovered that the fclose on a non-existant file (first time program is run there is no file) was causing the crash on my system. So I added the check mentioned below towards the very end of the read_from_file function so we only call fclose if we read in some data.

    Code:
    airline * read_from_file()
    {
        FILE *fp;
        airline *start, *curr, *temp;
        int length = 0;
        int airline_count= 0;
        int flight_count = 0;
        int passenger_count = 0;
    
        // Initialize pointers
        start = curr = NULL;
    
        // Open file for reading
        fp = fopen("airline.txt","rb");
    
        // Not necessary
        while(!feof(fp))
        {
            // Read in number of airlines stored in file
            if( fp == NULL)
                airline_count = 0;
            else
                fread(&airline_count,sizeof(int),1,fp);
    
            // Loop through "airline_count" airlines
            for( ; airline_count > 0; --airline_count )
            {
                // allocate room for a single airline structure and flight link to NULL
                temp = (airline *)malloc(sizeof(airline));
                temp->next = NULL;
                temp->node1 = NULL;
    
                // read airline name length
                fread(&length,sizeof(int),1,fp);
    
                //allocate sufficent memory for the airline name
                temp->airlinename = (char *)malloc(length+1);
    
                // read in airline's name and NULL terminate
                fread(temp->airlinename,length,1,fp);
                temp->airlinename[length] = 0;
    
                // Add current airline to end of growing list of airlines
                if( start == NULL ) 
                {
                    start = curr = temp;
                }
                else
                {
                    curr->next = temp;
                    curr = curr->next;
                }
    
                // Read in number of flights stored for the airline in file
                fread(&flight_count,sizeof(int),1,fp);
    
                // Loop through "flight_count" flight
                for( ; flight_count > 0; --flight_count )
                {
                    // allocate room for a single flight structure and set passenger link to NULL
                    curr->node1 = (flight *)malloc(sizeof(flight));
                    curr->node1->next = NULL;
                    curr->node1->node2 = NULL;
    
                    // read flight name length
                    fread(&length,sizeof(int),1,fp);
    
                    //allocate sufficent memory for the flight name
                    curr->node1->flightname = (char *)malloc(length+1);
    
                    // read in flight name and NULL terminate
                    fread(curr->node1->flightname,length,1,fp);
                    curr->node1->flightname[length] = 0;
    
                    // read departure location length
                    fread(&length,sizeof(int),1,fp);
    
                    //allocate sufficent memory for the departure location
                    curr->node1->departure_location = (char *)malloc(length+1);
    
                    // read in departure location and NULL terminate
                    fread(curr->node1->departure_location,length,1,fp);
                    curr->node1->departure_location[length] = 0;
    
                    // read arrival location length
                    fread(&length,sizeof(int),1,fp);
    
                    //allocate sufficent memory for the arrival location
                    curr->node1->arrival_location = (char *)malloc(length+1);
    
                    // read in arrival location and NULL terminate
                    fread(curr->node1->arrival_location,length,1,fp);
                    curr->node1->arrival_location[length] = 0;
    
                    // read take off time length
                    fread(&length,sizeof(int),1,fp);
    
                    //allocate sufficent memory for the take off time
                    curr->node1->take_off_time = (char *)malloc(length+1);
    
                    // read in take off time and NULL terminate
                    fread(curr->node1->take_off_time,length,1,fp);
                    curr->node1->take_off_time[length] = 0;
    
                    // Add current flight to end of growing list of flights
                    if( start->node1 == NULL ) 
                    {
                        start->node1 = curr->node1 = temp->node1;
                    }
                    else
                    {
                        curr->node1->next = temp->node1;
                        curr->node1 = curr->node1->next;
                    }
    
    			
                    // Read in number of passengers stored for that file
                    fread(&passenger_count,sizeof(int),1,fp);
    
                    // Loop through "passenger_count" passengers for this flight
                    for( ; passenger_count > 0; --passenger_count )
                    {
                        // allocate room for a single passenger structure
                        curr->node1->node2 = (passenger *)malloc(sizeof(passenger));
                        curr->node1->node2->next = NULL;
    
                        // read passenger name length
                        fread(&length,sizeof(int),1,fp);
    
                        //allocate sufficent memory for the passenger name
                        curr->node1->node2->name = (char *)malloc(length+1);
    
                        // read in passenger name and NULL terminate
                        fread(curr->node1->node2->name,length,1,fp);
                        curr->node1->node2->name[length] = 0;
    
    
                        // Add current passenger to end of growing list of
                        // passengers for this flight
                        if( start->node1->node2 == NULL ) 
                        {
                            start->node1->node2 = curr->node1->node2 = temp->node1->node2;
                        }
                        else
                        {
                            curr->node1->node2->next = temp->node1->node2;
                            curr->node1->node2 = curr->node1->node2->next;
                        }
                    }
                }
            }
        } // Not necessary
    
        // Should only have to close file if there was a file to read
        if( airline_count > 0 ) fclose(fp);
        return start;
    }
    That isn't the only problem of course. Once I made the above changes, I was able to add airlines, delete airlines and exit the program. I was then able to successfully restart the program and display the list of airlines that had been saved and read from the file. At this point I hadn't entered any flight or passenger information for any of the airlines. I added a flight to one of the airlines I exited the program and started the program again. Now when I displayed the list of airlines, the airline list was messed up. Attempting to exit the program at this point also seemed to be a little problematic. So, there seems to be an issue with either saving or reading from the file once you get to the point where you start reading/writing flight information.

    Another problem is all your menu functions seem to recursively calling themselves. This could eventually come back to bite you and should be fixed. As an example:

    Code:
    // Menu Function which is handles the airlinelist
    airline* airline_menu(airline *start)
    {                                                                      
        int choice;
        
        printf("Welcome to the Main Menu\n");
        printf("Please enter your choice from the menu shown below\n");
        printf("1: Add a new airline to this airport\n");
        printf("2: Delete an airline from the airport\n");
        printf("3: Display the Airlines List at this Airport\n");
        printf("4: Enter Flight menu for an airline\n");
        printf("5: Exit the program\n");
        scanf ("%d", &choice);
        switch(choice)
        {
            case 1: 
            { 
                start1 = create_airline_node(start1);
            } break;
            case 2:
            {  
                start1 = delete_airline_node(start1);
            } break;
            case 3:
            {
                print_airline_list(start1);
            } break;
            case 4:
            {
                start1 = enter_flight_menu(start1);
            } break;
            case 5:
            {
                write_to_file(start1);
                exit(1);
            } break;
            default:
            {
                start1 = airline_menu(start1);
            } break;
        }
        printf("\n");
        start1 = airline_menu(start1);
        return start1;
    }
    This function should be rewritten to look something like:

    Code:
    airline* airline_menu(airline *start)
    {                                                                      
        int choice;
        
        do
        {
            printf("Welcome to the Main Menu\n");
            printf("Please enter your choice from the menu shown below\n");
            printf("1: Add a new airline to this airport\n");
            printf("2: Delete an airline from the airport\n");
            printf("3: Display the Airlines List at this Airport\n");
            printf("4: Enter Flight menu for an airline\n");
            printf("5: Exit the program\n");
            scanf ("%d", &choice);
            switch(choice)
            {
            case 1: 
                start1 = create_airline_node(start1);
                break;
            case 2:
                start1 = delete_airline_node(start1);
                break;
            case 3:
                print_airline_list(start1);
                break;
            case 4:
                start1 = enter_flight_menu(start1);
                break;
            case 5:
                write_to_file(start1);
                break;
            default:
                break;
            }
            printf("\n");
        } while( choice != 5 );
        return start1;
    }
    There, no more recursion. However, I must point out that you are passing in a start pointer but everywhere in the code above you are using the global start1 pointer.

    It would also seem that you need to have a function that cleans up all the allocated memory for your linked lists. Something that gets called before main exits.

    Make the changes I made above and try to get a little bit further. Put in temporary printf statements to help debug your code. If you still have problems, post you code again and explain what part of your code you think isn't working correctly.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A slight correction to the first block of your follow-up post:
    Code:
    airline * read_from_file()
    {
        FILE *fp;
        airline *start, *curr, *temp;
        int length = 0;
        int airline_count= 0;
        int flight_count = 0;
        int passenger_count = 0;
    
        // Initialize pointers
        start = curr = NULL;
    
        // Open file for reading
        fp = fopen("airline.txt","rb");
    
        // Not necessary
        while(!feof(fp))
        {
    
    
        } // Not necessary
    
        // Should only have to close file if there was a file to read
        if( airline_count > 0 ) fclose(fp);
        return start;
    }
    There are a few problems here.

    1) The feof issue. For starters, just for clarification, you don't call feof until you have actually read something. Second, you shouldn't use feof to control a loop. This first comment is really to the origional poster.

    2) You should modify your code, or your check for closing the file. Don't base it on if your airline_count is something. Rather, base it on if fopen failed or not. A better way to write it would be:
    Code:
    airline * read_from_file()
    {
        FILE *fp;
        airline *start, *curr, *temp;
        int length = 0;
        int airline_count= 0;
        int flight_count = 0;
        int passenger_count = 0;
    
        // Initialize pointers
        start = curr = NULL;
    
        // Open file for reading
        fp = fopen("airline.txt","rb");
        if( fp != NULL )
        {
            ...your stuff here...
    
            fclose( fp );
        }
        return 0;
    }
    Or skip the braces pair, and just have it:
    Code:
        ...
    
        if( fp != NULL )
            fclose( fp );
    
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Wow, these 3 lines in a row

    > fflush(stdin);
    > node->airlinename = new char [80];
    > gets(node->airlinename);
    Ugh, fflush(stdin),
    argh!, c++ code on the C board,
    Barf - using gets() to read input!
    Holy syntax batman!

    Go read the multitude of FAQs

    Next time, try and narrow down the problem - few people can be bothered (or have the time) to wade through that much code.

    A better tip would be to make sure it works by thorough testing before you've dug such a big hole. Writing C is easy, getting it debugged is much harder. If you have too much untested code lying about, then it gets really tough (especially for newbies).

    Code:
    [number of airlines]
    [first airline's name]
        [number of flights for first airline]
        [first airline's, first flight's information]
        [first airline's, first flight's number of passengers]
            [first airline's, first flight's, first passengers information]
            [first airline's, first flight's, next passengers information]
    A useful idea would be to assign each level of the data structure it's own function (which can be tested in isolation).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. C programming-Linked list problem
    By male4329 in forum C Programming
    Replies: 18
    Last Post: 06-02-2005, 02:05 AM
  3. file & linked list run time error
    By Micko in forum C Programming
    Replies: 9
    Last Post: 03-06-2004, 01:58 AM
  4. Linked List problem
    By spec85 in forum C Programming
    Replies: 1
    Last Post: 06-14-2002, 03:58 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21