Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 10 years ago

Can anyone give me an example of usage of direct access files containing structures in C++?

I have to write a Program that reads information from a file with direct access and change some stuff in it. ( I have to save info about students in this file )

4 Answers

Relevance
  • 10 years ago
    Best Answer

    Direct access means that you can seek in a file according to an index file.

    1. file contains id, other i.e. human readable information, and an offset into the file

    2. file contains data with identical recordlengths

    So you would use the index file in order to identify the information you want to retrieve

    from the direct access file.

    // record structure for direct access

    struct Person {

    int id;

    char szName[30];

    char szAddress[60];

    ...

    };

    struct PersonIDX {

    int id;

    char szName[15]; // to have an idea who this could be

    unsigned offset;

    };

    In C you would use fread/fwrite, fseek and ftell in order to move around

    In C++ this would be an outline of an update function

    void update_patient()

    {

    Person p;

    PersonIDX idx;

    fstream f1, f11;

    int person_no = 0;

    f1.open( "person.dat" , ios::in|ios::out|ios::binary );

    f11.open ( "personIDX.dat", ios::in|ios::binary );

    cout<<"Please enter Person ID to modify";

    cin>>person_no;

    while(!f11.eof()){

    f11.read((char*)&idx,sizeof(idx));

    if(idx.id==person_no) {

    // get the update information

    p.getchanges();

    f1.seekp(idx.offset);

    f1.write((char*)&p,sizeof(p));

    break;

    }

    }

    f1.close();

    f11.close();

    }

  • 3 years ago

    1

    Source(s): Instant Telephone Reverse http://ReversePhoneNumberLookup.enle.info/?6nyP
  • 3 years ago

    the excellent 'debate' is what's a intense question? How do you gage somebody's sincerity? as quickly as you initiate off attempting to respond to questions like that then you definitely initiate off moving into censorship and whilst there are some aspects (to me) that are open and close no flow aspects in a public communicate board there are greater gray aspects then you definitely recognize. on the top of the day it truly is user-friendly adequate to pass questions which you do unlike, in actuality if greater people did that then there could be much less room for lots of the idiots/trolls who infect Yahoo solutions frequently. I relish non secular greater open ended questions the place there is a few room for debate or opinion. I save coming back, because of the fact I nonetheless discover it appealing how a majority of those people from diverse walks and a while of existence actual come at the same time and initiate off communicatings suggestions and ideology.

  • 10 years ago

    Research the following functions (a good spot would be www.cplusplus.com):

    - fopen(): open file for reading/writing/both

    - fscanf(): read from file

    - fprintf(): write to a file

    - fclose(): close the file that was opened by fopen()

    Define the structures, and then use those functions correctly to read/write the file.

Still have questions? Get your answers by asking now.