Good way to load/store lines from file?

This is a discussion on Good way to load/store lines from file? within the C++ Programming forums, part of the General Programming Boards category; I'm trying to make a routine that loads up entries from a file (delimited by a newline) into an STL ...

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    Good way to load/store lines from file?

    I'm trying to make a routine that loads up entries from a file (delimited by a newline) into an STL list. However, I'm getting a few errors:

    Code:
    void CDatabase::LoadFile(LPSTR FileToLoad)
    {
    	ifstream TheFile;
    
    	TheFile.open(FileToLoad,ifstream::in);
    The declaration of 'TheFile' gives the following error:
    c:\Documents and Settings\Ben Forbes\My Documents\Visual Studio Projects\misc2\music\CDatabase.cpp(23): error C2079: 'TheFile' uses undefined class 'std::basic_ifstream<_Elem,_Traits>'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    The call to 'TheFile.open()' gives a pretty similar error:
    [quote]
    c:\Documents and Settings\Ben Forbes\My Documents\Visual Studio Projects\misc2\music\CDatabase.cpp(26): error C2027: use of undefined type 'std::basic_ifstream<_Elem,_Traits>'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    [/code]
    I've included 'iostream' and specified 'using namespace std'. Can anyone see something wrong with my code so far?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You need to include 'fstream'.

    Edit:

    Also why do you use c-style strings? why not use STL string?
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Thanks. Seems like a bit of overhead to use STL strings.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I think the overhead is neglible compared to the ease of use. You are already using STL list. It's up to you but I'd be impressed if you did a comparison using c-style strings and STL strings and that was your bottle neck. Both will work though. Good luck.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    How about this clean up code:
    Code:
    CDatabase::~CDatabase()
    {
    	while (EntryList.front())
    	{
    		delete [] EntryList.front();
    		EntryList.pop_front();
    	}
    }
    Is the while expression a valid means of checking if the list is clear or not?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Nevermind that. My problem now, is that my entries are never getting deleted in the constructor:
    Code:
    CDatabase::~CDatabase()
    {
    	while (EntryList.size()>0)
    	{
    		delete [] EntryList.front();
    		EntryList.pop_front();
    	}
    }
    Before the destructor is called, I have confirmed that there are entries in the list, but when the above code is run, size() returns 0. Why is this?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I was popping the entries somewhere else, so that's fixed now.

    What is the best way to iterate through the list from front to back?

    EDIT: Nevermind me. Boy I really have to try a bit harder before posting! I solve all my problems immediately after I post.
    Last edited by bennyandthejets; 06-22-2004 at 01:59 AM.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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