Thread: please help

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    please help

    i have an assignment about file handling.

    i am saving data to file in binary format using following statement

    a.getdata();
    fpt = fopen("stock.dat", "ab");
    fwrite( &a, sizeof(a), 1, fpt );
    fclose(fpt);
    where a is object of class data

    for printing file contents my code is

    fpt = fopen("stock.dat", "r");
    while ( feof(fpt) == 0 )
    {
    fread( &a, sizeof(a), 1, fpt );
    a.putdata();
    }
    fclose(fpt);
    break;

    now problem is here, at time of printing file contents is showing last record twice. What should i do ?

    Also give me the idea to display the selected records using conditon given by user ( dbase style )

    Code is also attached with this thread.
    thnx.

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    It's shocking me how often this one is coming up.
    Look here;
    http://www.cprogramming.com/cboard/s...threadid=18036

    And at some of the other EOF threads in this forum.

    [edit]
    Sorry, there are a number of EOF threads in the C forum at the moment, not this one.
    [/edit]
    Last edited by Azuth; 05-21-2002 at 02:11 AM.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Okay, correct me if I am wrong, as my C++ is not as strong as my C, though it appears as though you are using the C file access commands.

    C++ has a differenct set of commands that should be used.

    These commands are:
    ofstream
    seek
    write
    ( Just to name a few )

    I would suggest implementing the propper C++ programming instead of trying to merge C and C++ programming.

    Anyway that is my 2 cents on the topic.

    Later,
    WebmasterMattD
    WebmasterMattD.NET

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    how to edit records...

    i somehow managed to append records and to display the entire file contents which is stored in binary format.
    Now i want to edit the particular record.
    i tried it in following manner
    1 . get record no from user
    2 . calculate address at which the pointer must be located
    3 . place pointer at desired location
    4 . read new info from user
    5 . update

    like this

    fpt.open("stock.dat", ios:: binary | ios :: ate | ios:ut );
    cout << "record no";
    cin >> rec;
    loc = (rec-1)*sizeof(a);
    if ( fpt.eof() )
    {
    fpt.clear();
    }
    fpt.seep(loc);
    a.getdata() // nre data
    fpt.write( ( char * )&a, sizeof a );
    fpt.close();

    but the respected recored is not updated
    please show me the way

Popular pages Recent additions subscribe to a feed