Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

FileIO in C++ problem.

Options
  • 01-04-2006 7:04pm
    #1
    Registered Users Posts: 2,528 ✭✭✭


    Hi, Im trying to read in from a file containing names and info (each on a seperate line) so I can create a linked list with the data.
    I need to read in the name followed by the info but when I put two getline statements one after the other it crashes.

    Any help would be much appreciated.
    cout << "Please enter filename: ";
    		string filename; 
    		getline(cin,filename);
    
    		ifstream myfile;
    		myfile.open (filename.c_str());
    
    		if (myfile.is_open())
    		{
    			node *temp, *temp2, *temp3; 
    
    			while (! myfile.eof() )
    			{
    
    				getline(myfile,temp->name);
                                    getline(myfile,temp->info);
    				// do stuff with this line.
    			}
    
    			myfile.close();
    		}
    		else
    			cout << "Unable to open file";
    


Comments

  • Registered Users Posts: 1,481 ✭✭✭satchmo


    You're creating pointers to node objects, but you're not creating any actual objects. When you write temp->name, you're dereferencing a pointer that doesn't point at anything, which is a Bad Thing.


  • Registered Users Posts: 2,528 ✭✭✭TomCo


    Ah, can't believe I didn't notice that!

    I was convinced it had something to do with the getline statements, I always overlook the obvious.

    Thanks.


  • Registered Users Posts: 2,528 ✭✭✭TomCo


    Crap, another problem cropped up.
    cout << "Please enter filename: ";
    		string filename; 
    		getline(cin,filename);
    
    		ifstream myfile;
    		myfile.open (filename.c_str());
    
    		if (myfile.is_open())
    		{
    			node *temp;
    
    			temp = new node;
    			while (!myfile.eof())
    			{
    				getline(myfile,temp->name);
    				getline(myfile,temp->info);
    
    				if (start_ptr == NULL)
    				{ 
    					start_ptr = temp;
    					start_ptr->nxt = NULL;
    				}
    
    				cout << start_ptr->name;
    				cout << start_ptr->info;
    			}
    				myfile.close();
    		}
    		else
    			cout << "Unable to open file";
    

    My input file is

    a
    a

    b
    b

    c
    c


    Im just trying to get it to correctly create the start_ptr to being with.
    start_ptr has a value of NULL initially and as far as I can see this code should only print "aaaaaa" but it prints "aabbcc".

    If I put in the rest of my code to create the ordered list the whole thing goes into an infinite loop. It works fine without the file input.


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    By saying start_ptr = temp, you're not assigning the value of temp to start_ptr, you're assigning the address. So saying start_ptr->name is the same as saying temp->name, which is why it's printing aabbcc.


Advertisement