reading/writing list

This is a discussion on reading/writing list within the C++ Programming forums, part of the General Programming Boards category; I am practicing writing a linked list to disk, and reading the data back into the list. The problem appears ...

  1. #1
    Unregistered
    Guest

    reading/writing list

    I am practicing writing a linked list to disk, and reading the data back into the list. The problem appears to be that the code is not writing the data to disk correctly. Any comments?


    #include <iostream>
    #include <fstream>
    #include <list>
    #include <cstdlib>

    using namespace std;

    class Test {
    private:
    int a;
    int b;
    public:
    Test( int var1, int var2 ) { a = var1; b = var2; }
    Test() { a = 0; b = 0; }
    //Test( list<Test>::iterator q ) { a = q->GetA(); b = q->GetB(); }
    int GetA() { return a; }
    int GetB() { return b; }
    };

    int main()
    {

    list<Test> mylist;
    list<Test>::iterator p;
    ofstream out("C:\\Dev-C++\\Program\\data.txt", ios:ut | ios::binary);
    ifstream in("C:\\Dev-C++\\Program\\data.txt", ios::in, ios::binary);

    // create the data
    mylist.push_back(Test(1,2));
    mylist.push_back(Test(3,4));

    p = mylist.begin();

    // write to disk
    while( p != mylist.end() ) {
    cout << "Writing a record to file...\n";
    out.write((char *) &p, sizeof(*p));
    p++;
    }

    mylist.clear(); // destroy the list

    in.close();

    // read from file into list
    mylist.push_back();
    p = mylist.begin();
    cout << "Reading a record from file...\n";
    in.read((char *) &p, sizeof(*p));

    while( !in.eof() ) {
    mylist.push_back();
    p++;
    cout << "Reading a record from file...\n";
    in.read((char *) &p, sizeof(*p));
    }

    in.close();

    p = mylist.begin();

    while( p != mylist.end() ) {
    cout << "A: " << p->GetA() << ' ' << p->GetB() << '\n';
    p++;
    }

    system("PAUSE");
    return 0;

    }

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You're writing from the address of the iterator, but you should be writing from the address that the iterator points to. This should work -

    Code:
    #include <iostream> 
    #include <fstream> 
    #include <list> 
    #include <cstdlib> 
    
    using namespace std; 
    
    class Test { 
    private: 
    int a; 
    int b; 
    public: 
    Test( int var1, int var2 ) { a = var1; b = var2; } 
    Test() { a = 0; b = 0; } 
    //Test( list<Test>::iterator q ) { a = q->GetA(); b = q->GetB(); } 
    int GetA() { return a; } 
    int GetB() { return b; } 
    }; 
    
    int main() 
    { 
    
    list<Test> mylist; 
    list<Test>::iterator p; 
    ofstream out("data.txt",  ios::binary);
    ifstream in("data.txt", ios::binary);
     
    // create the data 
    mylist.push_back(Test(1,2)); 
    mylist.push_back(Test(3,4)); 
    
    p = mylist.begin(); 
    
    // write to disk 
    while( p != mylist.end() ) { 
    cout << "Writing a record to file...\n"; 
    out.write((char *)&(*p), sizeof(*p)); 
    p++; 
    } 
    
    mylist.clear(); // destroy the list 
    
    out.close(); 
    
    
    // read from file into list 
    
    Test temp; 
    cout << "Reading a record from file...\n"; 
    in.read((char *)&temp, sizeof(temp)); 
    
    
    
    //mylist.push_back(p); 
    while( !in.eof() ) { 
    mylist.push_back(temp); 
    cout << "Reading a record from file...\n"; 
    in.read((char *) &temp, sizeof(temp)); 
    } 
    
    in.close(); 
    
    p = mylist.begin(); 
    
    while( p != mylist.end() ) { 
    cout << "A: " << p->GetA() << ' ' << p->GetB() << '\n'; 
    p++; 
    } 
    
    system("PAUSE"); 
    return 0; 
    
    }
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  3. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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