Thread: data read problem

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    21

    data read problem

    Well, I have trouble with reading data file by using binary search tree. I can't read file! Can some body help me out plz!!

    sample data file:
    Code:
    XYZ
    3
    John Go
    Supra YA
    Help ME
    My code here:
    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>
    
    // Definitions, this is a good idea to make things more readable
    #define MY_EOL 1
    #define ART_END 2
    #define MY_EOF 66
    #define DONTREAD 1
    #define TERMINATE 666
    
    // Data structures provided by and required by Ms. Traxler
    typedef char FLIGHT[7];
    typedef char TYPE_PLANE[3];
    typedef char CODE[4];
    typedef char NAME[51]; 
    
    typedef struct qnode
    {
       FLIGHT plane;
       struct qnode *next;
    }FNODE, *FPTR;
    
    typedef struct cnode *CPTR;
    typedef struct cnode
    {
       NAME controller;
       CPTR next;
       FPTR head;
    }CNODE;
        
    typedef struct 
    {
       CODE aircode;
       TYPE_PLANE type_plane[5];
    }DATA;
    
    typedef struct anode
    {
      DATA airport;
      struct anode *leftchild, *rightchild;
      CPTR controller_head, next_controller;
    }TREE_NODE, *TREEPTR; 
    
    
    // Group like functions together and seperate from other blocks
    void initialize_tree( TREEPTR *);
    void initialize_airport( TREEPTR);
    void initialize_controller( CPTR);
    void initialize_plane( FPTR);
    void initialize_name( DATA *);
    
    // Main driver functions
    int read_data(TREEPTR *);
    int read_airport(TREEPTR *, FILE *, char);
    int read_controller(CPTR *, FILE *, char []);
    
    // Utilities
    int get_airport(char [], int *, FILE *);
    int get_controller(char[], FILE *);
    
    
    int main(void)
    {
       TREEPTR root;
       int exit_control = 0;
    
    	initialize_tree(&root);
    
    	exit_control = read_data(&root);
    	
    	if (exit_control != TERMINATE)
    	{
    	  print list.....
                     }
    	
    	  
    	destroy tree......
    }
    
    void initialize_tree( TREEPTR *tree)
    {
    	*tree = NULL;
    }
    
    void initialize_airport( TREEPTR arprt)
    {
    	initialize_name(&(arprt->airport) );
    	arprt->controller_head = NULL;
    	arprt->leftchild = NULL;
    	arprt->rightchild = NULL;
    	arprt->next_controller = NULL;
    }
    
    void initialize_controller( CPTR contr)
    {
    	int k;
    	for (k = 0; k < 51; k++)
    		contr->controller[k]=NULL;
    	contr->head = NULL;
    	contr->next = NULL;
    }
    
    void initialize_plane( FPTR flight)
    {
    	int k;
    	for (k = 0; k < 7; k++)
    		flight->plane[k] = NULL;
    	flight->next = NULL;
    }
    
    void initialize_name( DATA *contrler)
    {
    	int i, j, k;
    
    	for (k = 0; k < 4; k++) 
    		(*contrler).aircode[k] = NULL;
    	for (i = 0; i < 3; i++)
    		for (j = 0; j < 5 ; j++)
    		(*contrler).type_plane[i][j] = NULL;
    	
    }
    
    /* Main Driver Functions */
    /*	read data
    Input: tree
    Output: updated tree, or error message
    Process: 
    	if inputs does not exist
    	  return no file error
    	else 
    	  if input is empty
    	    return empty input error
    	else
    	  while airport exists
    	    build airport database
    */
    int read_data(TREEPTR *root)
    {
    	FILE *infile, *aircraft;
    	char checkend, checkend1;
    	int return_value = 0;
    
    	printf("\n\nReading data, please wait ...\n\n");
    
    	infile = fopen("a.data","r");
    	if (infile == NULL)
    	{
    	   printf("Error.  Airport File does not exist.\n"
     		"Terminating Program.\n\n\n");
    	return_value = TERMINATE;
    	}
    	else
    	{
    	  checkend = fgetc(infile);
    	  
    	  if (EOF == checkend)
    	  {
    
    	    printf("Error! Airport file is Empty.\n"
    	           "Terminating Program.\n\n\n");
    	    return_value = TERMINATE;
    		  
    	  }
    	  else
    	  {
    	    while(EOF != checkend)
    	    {
    		read_airport(root, infile, checkend);
    		checkend = fgetc(infile);
    	    }
     
    	}
    
    	fclose(infile);
    	return return_value;
    }
    
    /*	read airport
    Input: airport list
    Output: new airport list, or error message
    Process  
    	initialize airport node
    	read airport 
    	read number of controllers
    	while controllers exist
    		build controller list 
    	insert controller list into airport node
    */	
    
    int read_airport(TREEPTR *root, FILE *infile, char letter)
    {
    	char airport_code[50];
    	int j, k, number=0;
    	TREE_NODE newAirport;
    	TREEPTR temp = NULL;
    
    	initialize_airport(&newAirport);
    	for (k = 0; k < 51; k++) airport_code[k] = NULL;
    
    	get_airport(airport_code, &number, infile);
    	
    
    	newAirport.airport.aircode[0] = letter;
    	strncat(newAirport.airport.aircode, airport_code, 49);
    	//for( j = 1; j <= number; j++)
    		read_controller(&newAirport.controller_head, infile, airport_code);
    
    	printf("Adding %s to the airport database\n\n", 
    			newAirport.airport.aircode);
    	insert_airport(root, newAirport);
    
    }
    
    
    
    
    /*	read controllers
    Input: controller list
    Output: updated controller list, or error message
    Process:
    	initialize time and controller node
    	get controller
    	insert controller node into controller list
    */
    int read_controller(CPTR *contr_head, FILE *infile, char airport_code[4])
    {
    	char contr_name[40];
    	int number=0;
    	CNODE the;
    
    
    	initialize_controller(&the);
    
    	get_controller(contr_name, infile);
    	
    	printf("Adding %s to airport %s\n", contr_name, airport_code);
    
    	strncpy(the.controller, contr_name, 40);
    
    	insert_contorller(contr_head, the);
    	
    }
    
    
    int get_airport(char airport_code[50], int *number, FILE *infile)
    {
    	int cond;
    	char word[50];
    
    	cond = fwordf(word, infile);
    		
     	strncpy(airport_code, word, 50);
    
    
    	while ( cond != ART_END)
    	{
    	  cond = fwordf(word, infile);
    	  strncat(airport_code, word, 50 - strlen(airport_code) );
    	  
    	}
    	
    	if (*number != DONTREAD)
    	{
    	  fscanf(infile, "%d", number);
    	  fgetc(infile);
    	} 
    
    }
    
    /*	get controller (Utility)
    input: conditional
    output: name of controller
    Process:
    	while end of line is not encountered
    	  read a word and append to the controller name
    
    	
    */
    int get_controller(char contr_name[50], FILE *infile)
    {
    	int cond = 0;
    	char word[50];
    
    	cond = fwordf(word, infile);
    
    	strncpy(contr_name, word, 50);
    
    	while( cond != MY_EOL)
    	{
    	  cond = fwordf(word, infile);
    	  strncat(contr_name, word, 50 - strlen(contr_name) );
    	}
    
    	
    }
    
    /*	fwordf
    fwordf is a utility function to read in a data file with specific parameters,
    i.e. words ending with ';', etc.
    */
    int fwordf(char *word, FILE *word_file)
    {
    	char letter;
    	int spaces = 0,
    		condition = 0, i = 0, k;
    
    	for (k = 0; k < 50; k++) *(word + k) = NULL;
    
    	while (spaces == 0)
    	{
    		letter = fgetc(word_file);
    		if ( letter != ' ' )
    				spaces = 1; 
    	}
    
    	if ('\n' == letter)
    		condition = MY_EOL;*/
    	else
    	{
    
    	  while (!isspace(letter) )
    	  {
    	    *(word + i) = letter;
    	    i++;
    	    letter = fgetc(word_file);
    
    	    if (letter == EOF)
    	    {
    		condition = MY_EOF;
    		letter = ' ';
    	    }
    	    else if (letter == ';') 
    		{ 
    			letter = ' ';
    			condition = ART_END;
    		}
    	  }
    	
    	}
    	  
    	  if (letter == '\n')
    		condition = MY_EOL;
    
    	*(word + i ) = ' ';
    	return condition;
    }
    Thanks for any help
    Last edited by Supra; 02-03-2002 at 07:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read in problem
    By florian100 in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2009, 06:33 AM
  2. Read size of data array when reading .txt data
    By Taquito in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 01:52 AM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. read data from file
    By matth in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2005, 09:37 AM
  5. C diamonds and perls :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM