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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

FileIO in C++ problem.

  • 01-04-2006 6:04pm
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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