Thread: I need some help

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    8

    I need some help

    OK...i dont know if its too early at work, or what...but i shouldnt be having problems reading in some data and storing them into linked lists...does anyone see any errors with this code?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream.h>
    #include <fstream.h>
    
    class int_node
    {
    	public:
    		long item_num;
    		int_node *next;
    };
    
    class text_node
    {
    	public:
    		char descr[150];
    		text_node *next;
    };
    
    int main()
    {
    	FILE *masterIntIn;
    	masterIntIn = fopen("C:\\Cplusplus\\dm\\bin\\master_int.txt" , "r");
    
    	FILE *itemLocateNumIn;
    	itemLocateNumIn = fopen("C:\\Cplusplus\\dm\\bin\\item_location_num.txt" , "r");
    
    	FILE *matchItemsOut;
    	matchItemsOut = fopen("C:\\Cplusplus\\dm\\bin\\matching.txt" , "w");
    
    	FILE *itemLocateDescrIn;
    	itemLocateDescrIn = fopen("C:\\Cplusplus\\dm\\bin\\item_location_descr.txt" , "r");
    
    	int_node *current_master;
    	int_node *root_master;
    	root_master = new int_node;
    	root_master->next = NULL;
    	current_master = root_master;
    
    	int_node *current_lnum;
    	int_node *root_lnum;
    	root_lnum = new int_node;
    	root_lnum->next = NULL;
    	current_lnum = root_lnum;
    
    	text_node *current_descr;
    	text_node *root_descr;
    	root_descr = new text_node;
    	root_descr->next = NULL;
    	current_descr = root_descr;
    
    	while ( !feof( itemLocateDescrIn ) )
    	{
    		char tempCh[150];
    		fgets( tempCh , itemLocateDescrIn , 150 );
    		strcpy( current_descr->descr , tempCh );
    	
    		current_descr->next = new Text_node;
    		current_descr = current_descr->next;
    		current_descr->next = NULL;
    	}	
    
    	while ( !feof( masterIntIn ) )
    	{
    		long tempLong;
    		fscanf( masterIntIn , "%ld" , &tempLong ); 
    		strcpy( current_master->item_num , tempLong );
    	
    		current_master->next = new Int_node;
    		current_master = current_master->next;
    		current_master->next = NULL;
    	}
    
    	while ( !feof( itemLocateNumIn ) )
    	{
    		long tempLong;
    		fscanf( itemLocateNumIn , "%ld" , &tempLong ); 
    		strcpy( current_lnum->item_num , tempLong );
    	
    		current_lnum->next = new Int_node;
    		current_lnum = current_lnum->next;
    		current_lnum->next = NULL;
    	}	
    
    	current_descr = root_descr;
    	current_master = root_master;
    	current_lnum = root_lnum;
    
    	while ( current_master->next != NULL )
    	{
    		printf("%l \n" , current_master->item_num);
    		current_master = current_master->next;
    	}
    
    	return 0;
    }
    thanks

    ~omega

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Is this the problem of outputting the last record twice, or some other problem?

    > while ( !feof( itemLocateNumIn ) )
    Why you shouldn't do this is in the FAQ
    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.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Since this is the C++ board, I would say you should try to stick with the C++ headers and C++ stream I/O; drop the FILE*, fopens and printfs in favor of ofstream/ifstream and cout. You can also simplify things greatly by switching to a STL container instead of your linked list.

    One error you have is this:

    Code:
    while ( !feof( masterIntIn ) )
    {
        long tempLong;
        fscanf( masterIntIn , "%ld" , &tempLong ); 
        strcpy( current_master->item_num , tempLong );
    	
        current_master->next = new Int_node;
        current_master = current_master->next;
        current_master->next = NULL;
    }
    
    while ( !feof( itemLocateNumIn ) )
    {
        long tempLong;
        fscanf( itemLocateNumIn , "%ld" , &tempLong ); 
        strcpy( current_lnum->item_num , tempLong );
    	
        current_lnum->next = new Int_node;
        current_lnum = current_lnum->next;
        current_lnum->next = NULL;
    }
    You do not use strcpy to copy a long value into another long value. A simple assignment (=) should be all you need.

    Try to use C++ to your advantage. If you follow my suggestion of using an STL container, the code to process the master_int file (for example) could be replaced with:

    Code:
    ifstream masterIntIn("C:\\Cplusplus\\dm\\bin\\master_int.txt");
    list<long> longList;  // Linked-list object to store values into
    
    // Make sure file was opened
    if( !masterIntIn.is_open() )
    {
        cerr << "Error, could not open master_int.txt file." << endl;
        exit(1);
    }
    
    // Read all values from "masterIntIn" file and store into "longList" linked-list object
    copy(istream_iterator<long>(masterIntIn),istream_iterator<long>(),back_inserter(longList));
    You would need to include <list>, <algorithm>, and possibly <iterator> to get the above code to work.
    "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

Popular pages Recent additions subscribe to a feed