Thread: Linked List with files problem - need help!

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    Question Linked List with files problem - need help!

    Hey everyone.

    I just joined this forum, so I might be doing something wrong, but I made a program with linked lists, but there are errors that occur:

    1. Whenever I try to save a new movie, it overwrite the previous data stored in the file. [FIXED]
    2. When the program exits, all information in movie.ini disappears (I have no idea why) [FIXED]
    3. I need to getlines to get the movie name. [FIXED]

    Code:
    #include <iostream>
    #include <string>
    #include <conio.h>
    #include <windows.h>
    #include <math.h>
    #include <time.h>
    #include <iomanip>
    #include <limits>
    #include <fstream>
    
    using namespace std;
    
    bool checkFile(char filename[255])
    {
         ifstream check (filename);
         if (check)
            return 0;
         else
            return 1;
    }
    
    typedef class movie
    {
            public:
            char *title;
            char *cat;
            char *genre;
            movie *next;
    };
    
    int mainMenu()
    {
        int option;
        
        cout << "+-----------------------------+" << endl;
        cout << "|          MAIN MENU          |" << endl;
        cout << "+-----------------------------+" << endl <<
                "| 1. Enter a new movie        |" << endl << 
                "| 2. Show media               |" << endl <<
                "| 3. Search media             |" << endl << 
                "| 4. Help                     |" << endl << 
                "| 5. About                    |" << endl <<
                "| 6. Quit                     |" << endl <<
                "+-----------------------------+" << endl <<
                "|     :ENTER AN OPTION:       |" << endl <<
                "+-----------------------------+" << endl;
        cin >> option;
        cin.ignore(numeric_limits<streamsize>::max(),'\n');
        return option;
    }
    
    void decide(int opt, movie *head)
    {
         string title;
         string cat;
         string genre;
         int a = 0;
         ifstream check ("movie.ini");
         if (!check)
         {
                    a = 1;
         }
         check.close();
         
         if (opt == 1)
         {
                 ofstream fOut ("movie.ini", ios::app);
                 cout << "Enter movie title: ";
                 getline(cin,title,'\n');
                 cout << "Enter movie category: ";
                 getline(cin,cat,'\n');
                 cout << "Enter movie genre: ";
                 getline(cin,genre,'\n');
                 if (a != 1)
                    fOut << endl;
                    
                 fOut << title << endl << cat << endl << genre;
                 fOut.close();
         }
         if (opt == 2)
         {
              int cnt = 1;
              movie *list;
              list = head;
              system("cls");
              cout << setw(3) << left << "#" << setw(40) << left << "Movie" <<  setw(17) << left << "Category" <<  setw(17) << left << "Genre\n" << endl;
              while (list != NULL)
              {
                    cout << setw(3) << left << cnt << setw(40) << left << list->title <<  setw(17) << left << list->cat <<  setw(17) << left << list->genre << endl;
                    list = list->next;
                    cnt++;
              }
         }
         if (opt == 4)
            cout << endl << endl << " -- Error opening help file!" << endl << endl;
         if (opt == 5)
            MessageBox(NULL,"(C) 1991-2006 Matt Nesterenko","About Media Manager",MB_OK);
         if (opt == 6)
            exit(0);
    }
    
    void menu(string menu, movie *head)
    {
         int opt;
         if (menu == "main")
            opt = mainMenu();
         decide(opt, head);
    }
    
    int main (int argc, char *argv[])
    {
        movie *head;
        movie *mNode;
        movie *tail;
    
        while (1)
        {
             ifstream input( "movie.ini");
             
             head = tail = NULL;
             
             if (input)
             {
                 while (! input.eof())
                 {
                      mNode = new movie;
                      mNode->title = new char[255];
                      mNode->cat = new char[255];
                      mNode->genre = new char[255];
                      mNode->next = NULL;
                      input.getline(mNode->title, 255, '\n');
                      input.getline(mNode->cat, 255, '\n');
                      input.getline(mNode->genre, 255, '\n');
                      if (head ==NULL)
                         head = mNode;
                      else
                          tail->next = mNode;
                      tail = mNode;
                 }
             }
             menu("main", head);
             cout << endl << endl;
             system("pause");
             system("cls");
        }
        
        system("pause");
        return 0;
    }
    Last edited by guitarist809; 03-27-2006 at 12:58 PM.

  2. #2
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    P.S. My compiler is DEV-C++ 4.9.9.2

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    mNode = new movie;
    mNode->title = (char *) malloc(255);
    Why are you mixing new and malloc? You really shouldn't even be using malloc in C++.
    Sent from my iPad®

  4. #4
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Well, because - I don't know what the C++ syntax is that is for a malloc().

    It would be great to know

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    The syntax is new. ...and the big reason you're having a problem is because you're using character pointers rather than std::strings, like you should be in C++.

    Code:
    #include <string>
    
    struct yourStruct {
       std::string this_is_a_string_object;  // No need to allocate memory, it's a solid object 
                                             // that can store a string as long as you want it.
       yourStruct *next;
    };
    You should get yourself a C++ book, you're looking at this too much from a C perspective and it's getting you a little screwed up. C++ is not C. There are ways to do things in C and much better ways to do it in C++.
    Sent from my iPad®

  6. #6
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by SlyMaelstrom
    The syntax is new. ...and the big reason you're having a problem is because you're using character pointers rather than std::strings, like you should be in C++.

    Code:
    #include <string>
    
    struct yourStruct {
       std::string this_is_a_string_object;  // No need to allocate memory, it's a solid object 
                                             // that can store a string as long as you want it.
       yourStruct *next;
    };
    You should get yourself a C++ book, you're looking at this too much from a C perspective and it's getting you a little screwed up. C++ is not C. There are ways to do things in C and much better ways to do it in C++.
    Thx.

    A question:
    1. is there a new(255) or is the new dynamic?


    cant I just do::

    Code:
    struct a
    {
    string *myString; //the string
    a *next;
    }
    or, should my strings not even be pointers?

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    No, there is no need to make your string a pointer. The only reason you have character pointers currently is because you want them to hold more than one character. A string object can do that already.

    And yes, with new, the proper syntax would be
    Code:
    char *foo = new char[255];
    
    // ...and don't forget
    
    delete[] foo;
    Sent from my iPad®

  8. #8
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    well, my main problem is with these lines of code:

    Code:
    if (opt == 2)
         {
              int cnt = 1;
              movie *list;
              list = head;
              while (list != NULL)
              {
                    cout << cnt << ". " << list->title << "\t" << list->cat << "\t" << list->genre << endl;
                    list = list->next;
                    cnt++;
              }
         }
    They erace all the contents of the file!

  9. #9
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    [ISSUE FIXED]

    > Error was caused from the ofstream being outside of the if (opt==1) circumstance, causing
    -> It to overwrite the file, but without any new data!

    [NEW ISSUE]
    When I add a new entry, the file gets overwritten

    is there an ios::"Don't overwrite" for the fstream

    I need to continue with the file, not overwrite it. anyone know how to resolve this?

  10. #10
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    A new issue has been added
    ^
    |

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quite impressive for code that doesn't interact with any ofstreams whatsoever. No, your actual problem lies here:
    Code:
    ofstream fOut ("movie.ini");
    A seemingly harmless line of code, right? Well consider this code:
    Code:
    #include <fstream>
    
    int main() {
       std::ofstream myFile("myTextFile.txt");
       
       myFile << "Look at all this text! ";
       myFile.close();
       myFile.open("myTextFile.txt");
       myFile << "Some more text!";
       
       return 0;
    }
    So what does the file say? Does it say "Look at all this text! Some more text!"? You would think so, right? You did, after all, write that to the file. Unfortunately, ofstream's default is to truncate the file, erasing the contents every time you open it. (every time you call the function), so this text output will actually generate a file that simply says "Some more text!". What you're looking for is:
    Code:
    ofstream fOut ("movie.ini", ios::app);
    Sent from my iPad®

  12. #12
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    OMG THANK YOU!!!! THAT FIXED THE PROBLEM!!

    Now, is this a bug or something:
    cout << "Enter movie title: ";
    getline(cin,title,'\n'); //For some reason, I need two of these!
    getline(cin,title,'\n');

    I have to put two getlines to get the movie title, while for the other getlines, i only need one. whats the issue here?

  13. #13
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    is it a C++ bug?

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    No, it's not a bug. It can all be traced back to this:

    Code:
    cin >> option;
    The cin >> call leaves a trailing newline character '\n' in the input stream. This then gets eaten by the next getline function call which of course sees the newline and therefore thinks the user pressed the "enter" key and continues on without actually getting any data into your title variable the first time the function is called. The second call doesn't find a newline character and so it waits for user input and successfully retrieves the title information entered.

    The solution to this is to throw away that trailing newline character after any cin >> that comes before a getline call.

    Code:
    cin >> option;
    cin.ignore(numeric_limits<streamsize>::max(),'\n');
    You'll need to include <limits> for that numeric_limits.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  15. #15
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Thank you all for the help! It really payed off, my program works well now -- thx!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  3. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM