Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

FileIO in C++ problem.

  • 01-04-2006 07: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