reading data from a file - link list

This is a discussion on reading data from a file - link list within the C++ Programming forums, part of the General Programming Boards category; i would like to write a main for my class -> my problem is that i really have no idea ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    141

    reading data from a file - link list

    i would like to write a main for my class -> my problem is that i really have no idea how to read data from a file..
    this is my class file ...

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    struct node
    {
       string IPaddress;
       int count;
       node *link;
    };
    
    class IPlist
    {
       // contains an ordered list of IP addresses and a count of each
       // occurrence
       public:
    
        IPlist();  // default constructor
                   // creates a new empty list
    
        ~IPlist(); // destructor - reallocates all dynamic memory of list
    
        IPlist (const IPlist & other);
                   // copy constructor to create a deep copy of other IPlist
                   // object
    
        bool empty() const; 
                   // precondition : none
                   // postcondition : returns true if list is empty 
                   //                 and otherwise false
    
        void insertInOrder(string address);
                   // precondition : list is ordered and address is not in list
                   // postcondition : list is ordered and contains address
    
        bool isPresent(string address) const;
                   // precondition : none
                   // postcondition : returns true if a node containing address
                   //                is present in list and otherwise false
    
        void updateCount(string address);
                   // precondition : address is in list
                   // postcondition : the count of node with address has been incremented
    
        node *find (string address) const;
                   // precondition : none
                   // postcondition : returns a pointer to node containing address or
                   //                 NULL if not present
    
        void display() const; 
                   // precondition : none
                   // postcondition : the list has been displayed 
                   // one record per line, tab-separated
                   // with heading IPaddress Count
        private:
        
           node *list;
    };
    any idea will be really appreciated

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    this is my main code but got some error...

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	ifstream myFile;           //input file stream variable
        char filename[40];         //filename is used to store the name of the input file
        
         
        cout<<"Enter name of file : ";
        cin>>filename;
        myFile.open(filename); //open the input file 
        
        while(myFile.fail()) //if open did not fail
        {
            cout << "Invalid file name" << endl;
            cout << "Enter name of file : " ; //read the name of the input file
            myFile.open(filename); //open the input file 
        }
        while(!myFile.eof())
        {
            myFile>> filename;
            cout << filename << endl;
        }
        
        system("pause");
        return 0;
    }
    the content of the .txt file is

    192.149.089.061

    100.001.004.031

    034.056.078.012

    192.149.089.061
    100.001.004.031

    192.149.089.061

    111.022.033.004

    192.149.089.061

    111.022.033.004

    111.022.033.004

    but after compile the program, the output is as below...

    192.149.089.061

    100.001.004.031

    034.056.078.012

    192.149.089.061
    100.001.004.031

    192.149.089.061

    111.022.033.004

    192.149.089.061

    111.022.033.004

    111.022.033.004

    111.022.033.004

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    That's why they tell in the FAQ that it is not a good idea to test for EOF
    Code:
    while(myFile>> filename) {
            cout << filename << endl;
    }
    is the preferred solution.
    Kurt
    EDIT:
    Code:
        while(myFile.fail()) //if open did not fail
        {
            cout << "Invalid file name" << endl;
            cout << "Enter name of file : " ; //read the name of the input file
            myFile.open(filename); //open the input file 
        }
    this while loop will not do what you expect it to do. Once a stream has failed it will not become good anymore unless you call clear() before open().
    Last edited by ZuK; 10-25-2006 at 08:29 AM.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    Quote Originally Posted by ZuK
    That's why they tell in the FAQ that it is not a good idea to test for EOF
    Code:
    while(myFile>> filename) {
            cout << filename << endl;
    }
    is the preferred solution.
    Kurt
    EDIT:
    Code:
        while(myFile.fail()) //if open did not fail
        {
            cout << "Invalid file name" << endl;
            cout << "Enter name of file : " ; //read the name of the input file
            myFile.open(filename); //open the input file 
        }
    this while loop will not do what you expect it to do. Once a stream has failed it will not become good anymore unless you call clear() before open().

    is this wat you mean


    Code:
        while(myFile.fail()) //if open did not fail
        {
            myFile.clear();
            cout << "Invalid file name" << endl;
            cout << "Enter name of file : " ; //read the name of the input file
            myFile.open(filename); //open the input file 
        }

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    yes.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    one more problem ...

    Code:
        while(myFile.fail()) //if open did not fail
        {
           myFile.clear();
            cout << "Invalid file name" << endl;
            cout << "Enter name of file : " ; //read the name of the input file
            myFile.open(filename); //open the input file 
        }
    the problem is when the filename is incorrectly inserted or no such filename, the display keep looping, which display
    Enter name of file : Invalid file name unstop

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    you have to input a new filname inside the loop.
    Code:
        while(myFile.fail()) //if open did not fail
        {
           myFile.clear();
            cout << "Invalid file name" << endl;
            cout << "Enter name of file : " ; //read the name of the input file
           cin >> filename;
            myFile.open(filename); //open the input file 
        }

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    opss... how could i miss that.. thank you anyway.. really appreciate the help although is a small problem .. cause i am really tired right now .. thank you dude...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. file & linked list run time error
    By Micko in forum C Programming
    Replies: 9
    Last Post: 03-06-2004, 01:58 AM
  4. Replies: 1
    Last Post: 03-02-2003, 07:42 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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