help with linked lists and file i/o

This is a discussion on help with linked lists and file i/o within the C++ Programming forums, part of the General Programming Boards category; for a school compsci assignment my class was asked to make a simple database program to keep track of a ...

  1. #1
    Registered User Mazer's Avatar
    Join Date
    Jul 2002
    Posts
    9

    help with linked lists and file i/o

    for a school compsci assignment my class was asked to make a simple database program to keep track of a user's mp3 collection. the program must be able to read a list of mp3s from a file, save the list to a file, sort the list, add songs, remove songs, and whatever other things i can come up with. i decided to use linked lists and for the most part it was working fine. my problem however, comes when reading and writing to a file.

    when trying to load a file, it gets stuck inside the loop (never reaching the iFile.eof() in order to exit). i can't figure out what's wrong.

    i've attached the entire source so you can try to understand what's going on, but the loadList() function is the focus of my problem:
    Code:
    void loadList()
    {
    	char file [40];
    	char junk [40];
    	newList(); // to delete any songs already loaded
    	cout << "Input the filename of the playlist to be loaded:\n";
    	cin.getline (file, 40,'\n');
    		
    	ifstream iFile (file);
    	
    	if (!iFile.is_open())
    	{
    		cout << "You wreckless fool of a Took! That file doesn't exist!\n";
    		return;
    	}
    
    	root = new mp3t;
    	position = root;
    	position->prev = NULL;
    	position->next = NULL;
    
    	iFile.getline (position->title,40, '\n');
    	iFile.getline (position->artist, 40, '\n');
    	iFile.getline (position->filename, 40, '\n');
    	iFile >> position->filesize;
    	iFile >> position->length;
    	iFile >> position->year;
    	iFile.getline (junk, 40, '\n');
    	
    	while (!iFile.eof())
    	{
    		position->next = new mp3t;
    		position->next->prev = position;
    		position = position->next;
    		position->next = NULL;
    		
    		iFile.getline (junk, 40, '\n');
    		iFile.getline (position->title, 40, '\n');
    		iFile.getline (position->artist, 40, '\n');
    		iFile.getline (position->filename, 40, '\n');
    		iFile >> position->filesize;
    		iFile >> position->length;
    		iFile >> position->year;
    		iFile.getline (junk, 40, '\n');
    	}	
    	iFile.close();	
    }
    and here's the text file to load:
    blah blah blah!
    nobody cares
    its a file.mp3
    213465
    100001
    1999
    blah blah blah!2
    nobody cares2
    its a file.mp32
    213465212
    100001212
    1999212
    blah blah blah!3
    nobody cares3
    its a file.mp33
    213465213
    100001213
    1999213

    any help would be greatly appreciated, thanks.

    PS, you can just ignore the humour, it's pointless right now, but once the program works i know the teacher will like it (besides, i can't help it)

  2. #2
    Registered User Mazer's Avatar
    Join Date
    Jul 2002
    Posts
    9
    ok, thanks for the help everyone

    if any of the 10 people who bothered to read this are interested, it turns out the idiot who sits next to me added a semicolon after the while statement which somehow screwed up the rest of the program. very weird... , but at least it works now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using a linked list globally and File I/O
    By Leftos in forum C Programming
    Replies: 46
    Last Post: 01-07-2008, 10:21 AM
  2. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 10:32 PM
  4. Linked lists and file i/o, and some other stuff
    By ninja in forum C++ Programming
    Replies: 9
    Last Post: 05-19-2002, 07:15 PM
  5. Saving and Loading Linked Lists With File i/o
    By Red Army in forum C++ Programming
    Replies: 8
    Last Post: 05-15-2002, 03:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21