Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Input multiple data types from a file into a linked list

Input multiple data types from a file into a linked list

Scheduled Pinned Locked Moved C / C++ / MFC
databasedata-structureshelpannouncement
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Farhan_Karim
    wrote on last edited by
    #1

    i am working on a movie database and i have output from my file that i want to load into my linked list,no problem opening file but when i run this function it enters junk values in the list and only works if i enter all the strings without spaces or seperated by special characters. i am reading this from the txt file SHAWSHANK REDEMPTION 120 5 MYSTERY 2009 ENGLISH 3 THE MATRIX 120 5 MYSTERY 2009 ENGLISH 3 THE MATRIX PART1 120 5 MYSTERY 2009 ENGLISH 3 THE MATRIX PART2 120 5 MYSTERY 2009 ENGLISH 3 this is the code for load text from file into a linkedlist(doubly)

    void double_llist::loadfromfile(){

    string mname,mgenre,mlanguage;
    int mrelease\_year,mamount, mrating,mruntime;
        ifstream fromfile;
        fromfile.open("database.txt");
        while (fromfile >> mname >> mgenre >> mlanguage >> mrelease\_year>>mamount>>mrating>>mruntime)
    {
        /\* do something with name, var1 etc. \*/
        cout << mname<
    

    the code for doublylinkedlist

    void double_llist::create_list(string mname,
    string mgenre,
    string mlanguage,
    int mrelease_year,
    int mamount,
    int mrating,int mruntime)
    {

    struct node \*s, \*temp;
    temp = new(struct node);
    temp->info=id++;
    temp->name = mname;
    temp->genre=mgenre;
    temp->language=mlanguage;
    temp->release\_year=mrelease\_year;
    temp->amount=mamount;
    temp->rating=mrating;
    temp->runtime=mruntime;
    temp->next = NULL;
    if (start == NULL)
    {
        temp->prev = NULL;
        start = temp;
    }
    else
    {
        s = start;
        while (s->next != NULL)
            s = s->next;
        s->next = temp;
        temp->prev = s;
    }
    

    }

    my class and node struct for linkedlist

    #include
    #include
    #include
    #include
    #include
    /*
    * Node Declaration
    */
    using namespace std;
    struct node
    {
    int info;
    string name;
    string genre;
    string language;
    int release_year;
    int amount;
    int rating;
    int runtime;
    struct node *next;
    struct node *prev;
    static int counter;
    }*start;
    int id=1;
    /*
    Class Declaration
    */
    class double_llist
    {
    public:

        void create\_list(string mname,
    string mgenre,
    string mlanguage,
    int mrelease\_y
    
    L D 2 Replies Last reply
    0
    • F Farhan_Karim

      i am working on a movie database and i have output from my file that i want to load into my linked list,no problem opening file but when i run this function it enters junk values in the list and only works if i enter all the strings without spaces or seperated by special characters. i am reading this from the txt file SHAWSHANK REDEMPTION 120 5 MYSTERY 2009 ENGLISH 3 THE MATRIX 120 5 MYSTERY 2009 ENGLISH 3 THE MATRIX PART1 120 5 MYSTERY 2009 ENGLISH 3 THE MATRIX PART2 120 5 MYSTERY 2009 ENGLISH 3 this is the code for load text from file into a linkedlist(doubly)

      void double_llist::loadfromfile(){

      string mname,mgenre,mlanguage;
      int mrelease\_year,mamount, mrating,mruntime;
          ifstream fromfile;
          fromfile.open("database.txt");
          while (fromfile >> mname >> mgenre >> mlanguage >> mrelease\_year>>mamount>>mrating>>mruntime)
      {
          /\* do something with name, var1 etc. \*/
          cout << mname<
      

      the code for doublylinkedlist

      void double_llist::create_list(string mname,
      string mgenre,
      string mlanguage,
      int mrelease_year,
      int mamount,
      int mrating,int mruntime)
      {

      struct node \*s, \*temp;
      temp = new(struct node);
      temp->info=id++;
      temp->name = mname;
      temp->genre=mgenre;
      temp->language=mlanguage;
      temp->release\_year=mrelease\_year;
      temp->amount=mamount;
      temp->rating=mrating;
      temp->runtime=mruntime;
      temp->next = NULL;
      if (start == NULL)
      {
          temp->prev = NULL;
          start = temp;
      }
      else
      {
          s = start;
          while (s->next != NULL)
              s = s->next;
          s->next = temp;
          temp->prev = s;
      }
      

      }

      my class and node struct for linkedlist

      #include
      #include
      #include
      #include
      #include
      /*
      * Node Declaration
      */
      using namespace std;
      struct node
      {
      int info;
      string name;
      string genre;
      string language;
      int release_year;
      int amount;
      int rating;
      int runtime;
      struct node *next;
      struct node *prev;
      static int counter;
      }*start;
      int id=1;
      /*
      Class Declaration
      */
      class double_llist
      {
      public:

          void create\_list(string mname,
      string mgenre,
      string mlanguage,
      int mrelease\_y
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      A couple of minutes with the debugger would have shown you that you cannot enter text like that and expect the system to realise that the film title comprises more than one word. Stream input breaks fields up at white space so in your first case you will have something like:

      mname = SHAWSHANK
      mgenre = REDEMPTION
      mlanguage = 120
      mrelease_year = 5
      mamount = MYSTERY - which is not an integer
      mrating = 2009
      mruntime = ENGLISH - also not an integer

      the final 3 will now go into the title next time round the loop.

      F 1 Reply Last reply
      0
      • L Lost User

        A couple of minutes with the debugger would have shown you that you cannot enter text like that and expect the system to realise that the film title comprises more than one word. Stream input breaks fields up at white space so in your first case you will have something like:

        mname = SHAWSHANK
        mgenre = REDEMPTION
        mlanguage = 120
        mrelease_year = 5
        mamount = MYSTERY - which is not an integer
        mrating = 2009
        mruntime = ENGLISH - also not an integer

        the final 3 will now go into the title next time round the loop.

        F Offline
        F Offline
        Farhan_Karim
        wrote on last edited by
        #3

        yes i know that's why i am asking a solution for that.

        L L 2 Replies Last reply
        0
        • F Farhan_Karim

          yes i know that's why i am asking a solution for that.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You need to split the lines manually so that you can separate the title from the other fields. You could just read the entire line, tokenise it into an array, and then work backwards field by field. Whatever is left at the beginning of the array can be recombined into a single string which is the title.

          1 Reply Last reply
          0
          • F Farhan_Karim

            i am working on a movie database and i have output from my file that i want to load into my linked list,no problem opening file but when i run this function it enters junk values in the list and only works if i enter all the strings without spaces or seperated by special characters. i am reading this from the txt file SHAWSHANK REDEMPTION 120 5 MYSTERY 2009 ENGLISH 3 THE MATRIX 120 5 MYSTERY 2009 ENGLISH 3 THE MATRIX PART1 120 5 MYSTERY 2009 ENGLISH 3 THE MATRIX PART2 120 5 MYSTERY 2009 ENGLISH 3 this is the code for load text from file into a linkedlist(doubly)

            void double_llist::loadfromfile(){

            string mname,mgenre,mlanguage;
            int mrelease\_year,mamount, mrating,mruntime;
                ifstream fromfile;
                fromfile.open("database.txt");
                while (fromfile >> mname >> mgenre >> mlanguage >> mrelease\_year>>mamount>>mrating>>mruntime)
            {
                /\* do something with name, var1 etc. \*/
                cout << mname<
            

            the code for doublylinkedlist

            void double_llist::create_list(string mname,
            string mgenre,
            string mlanguage,
            int mrelease_year,
            int mamount,
            int mrating,int mruntime)
            {

            struct node \*s, \*temp;
            temp = new(struct node);
            temp->info=id++;
            temp->name = mname;
            temp->genre=mgenre;
            temp->language=mlanguage;
            temp->release\_year=mrelease\_year;
            temp->amount=mamount;
            temp->rating=mrating;
            temp->runtime=mruntime;
            temp->next = NULL;
            if (start == NULL)
            {
                temp->prev = NULL;
                start = temp;
            }
            else
            {
                s = start;
                while (s->next != NULL)
                    s = s->next;
                s->next = temp;
                temp->prev = s;
            }
            

            }

            my class and node struct for linkedlist

            #include
            #include
            #include
            #include
            #include
            /*
            * Node Declaration
            */
            using namespace std;
            struct node
            {
            int info;
            string name;
            string genre;
            string language;
            int release_year;
            int amount;
            int rating;
            int runtime;
            struct node *next;
            struct node *prev;
            static int counter;
            }*start;
            int id=1;
            /*
            Class Declaration
            */
            class double_llist
            {
            public:

                void create\_list(string mname,
            string mgenre,
            string mlanguage,
            int mrelease\_y
            
            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Farhan_Karim wrote:

            works if...all the strings...seperated by special characters.

            Is that not a possibility?

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            1 Reply Last reply
            0
            • F Farhan_Karim

              yes i know that's why i am asking a solution for that.

              L Offline
              L Offline
              leon de boer
              wrote on last edited by
              #6

              The easy solution is to override the read and write methods in your own stream extension. It is the "standard", "usual" and "prefered" way to fix behaviour of classes or objects with methods and functions that aren't exactly as you desire. Can I go so far as to suggest MovieNameStream as a possible title for the extension.

              In vino veritas

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups