Thread: my homework (on crack)

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

    my homework (on crack)

    well, i was assigned my first project....but it's rather dull, so i decided to go WAY overboard on it.....

    i created a random access file to store events for a calendar program..events contains the date and a description of it...

    i'll try to post a little code as i can....i'm not asking you to find the bug...am i asking how i would find the bug...the biggest problem i'm facing is that i can't read the file (as a human)....

    the output and description of the error is below the code.

    Code:
    int FileManager::addRecord(Record &srcRec)
    {
        srand(56);     //really out of place, just haven't decided where
                             //to put it yet..i should probably use the time too
        
        srcRec.setKey(uniqueKey());
        int openRecord = firstAvailable();
        ofstream fout(filename, ios::binary);
        
        fout.seekp(openRecord * sizeof(Record));
        fout.write(reinterpret_cast<const char *>(&srcRec),
                    sizeof(Record));
        
        fout.close();    
    }   
    
    
    // i'm not planning on keeping this function, it's just for testing
    void FileManager::print(void)
    {
       
        ifstream fin;
        fin.open(filename, ios::binary);
        while(fin && !fin.eof())
        {
            fin.read(reinterpret_cast<char *>(&cur_rec),
                       sizeof(Record));
            cout << endl << endl;
            cout << "Key: " << cur_rec.getKey() << endl;
            cout << "Date: " << cur_rec.getMonth()<<"/"<<
                            cur_rec.getDay()<<"/"<< cur_rec.getYear() << endl;
            cout << "Label: " << cur_rec.getLabel() << endl;
            cout << "Desc: " << cur_rec.getDesc() << endl;
            
        }    
        fin.close();
    }
    Code:
    the only record i added was the one with the key 221....
    and for some reason it prints twice...i can't tell if this
    is a looping error in the print, or in the write...
    
    Key: 0
    Date: 0/0/0
    Label:
    Desc:
    
    
    Key: 221
    Date: 1/13/1999
    Label: xxxx
    Desc: nnnn
    
    
    Key: 221
    Date: 1/13/1999
    Label: xxxx
    Desc: nnnn
    Last edited by misplaced; 10-11-2004 at 03:38 PM.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Don't use eof() to control your loop in that way. The eof() returns true after you attempt to read past the end of the file. The check for eof() should therefore be immediately after the attempt to read.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    it's a fixed-size file. the file is sizeof(Record) * numberOfEntries...granted the file is not corrupt, reading it that way will always take me right to the end and eof() becomes true.

    say it has 4 entries, 100 bytes a piece...file is 400 bytes....
    loop 1: not eof() { read 0-100 bytes; //cursor is at 100 }
    loop 2: not eof() { read 100-200 //cursor is at 200 }
    loop 3: not eof() { read 200-300 //cursor is at 300 }
    loop 4: not eof() { read 300-400 //cursor is at 400 (eof) }
    loop 5: eof() is true;

    am i wrong?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    oh.....i am right....but you're saying that i WANT to try to read passed eof()???

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    loop 4: not EOF { read 300-400 //cursor is at 400 (eof) }
    loop 5: at but not past EOF so rest of loop input is undefined
    loop 6: past EOF so eof() is true;

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    well i'll be damned.....

    sorry...i guess i should have read the FAQ....

    i just disregarded anything like that because i figured trying to read past the end of file would cause a segmentation error or something....


    so before i go changing 5 or 6 different (mostly) working functions, would you guys agree that if i read once before the loop it would solve my problem?

    (that and putting the 'loop read' after the printing)

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    No, that wouldn't solve the problem by itself. To solve the problem you must run the check for eof() immediately after the call to read. There are a bunch of different ways to do that that may or may not include reading once before the loop.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>To solve the problem you must run the check for eof() immediately after the call to read.
    My favourite method goes as follows:
    Code:
    read some stuff();
    while(!file.eof())
    {
    process input();
    read some stuff();
    }
    It may not look very intuitive (because processing goes before reading in the loop), but it works and doesn't require a break or extra test condition.

    **EDIT**
    Just make sure that you're not doing any processing in between reads.. otherwise you'll still need the check-and-break method.
    Last edited by Hunter2; 10-11-2004 at 04:25 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM