Thread: what does this mean to you?

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    25

    Question what does this mean to you?

    this is for my data structures C++ class. this is the lab description. I feel pretty confused. can anyone tell me what this would mean to you?

    "Change your sequential file into a relative file, then modify the 14th records, and finally output the changed relative file in
    reverse order."

    i have the sequential file (a text file with 3 columns of entries, 75 entries long. any help?

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    I may be stating the obvious but it sounds like the file needs sorting depending on how the records in the file relate to each other.

  3. #3
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Hmm.

    I THINK that refers to storing data in RAX files, that is files with fixed field widths that can be subscripted and accessed non-sequentially. I have no idea how one would go about doing this.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    yeah, i have no idea either. i never really learned this stuff before. so do i use a linked list or something? i am so confused.
    I Love Jesus

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    The data could be held in a linked list or an STL vector (probably better, easiar to reverse).As for the file IO I have no idea.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    if you want fixed length records for random access i/o then you need to convert your text file to a binary file. Easy with structures.
    look into istream::read and ostream::write
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    i found out that structs are the best way to do this. i don't know anything about those though. i found this.

    The project consists of loading a random file.


    Students using The C Language, Pascal, Basic, or C++ will create a Relative File using the following steps.

    Students using other languages that support Random File Systems will use system file utilities to create a Random File, RNDMST. Include documentation of the processes used and a printout of file RNDMST.

    1. Create the sequential file RNDSEQ using the following records. Use an editor or download.
    Format:

    [sample data i deleted]


    Field and line delimiters other than commas may be used, i.e. TAB or SPACE.
    2. Print the contents of file RNDSEQ. Use a system file utility such as PRINT or CAT.
    3. *Create the Relative File, RNDMST using the records in file RNDSEQ. Write a program.
    4 Print the contents of file RNDMST in human readable form. Use a system file utility such as PRINT or CAT, or code print capability in your program. Print all 100 records if possible.
    5. Print the source code of the program used to create RNDMST in step 3. Use a system file utility such as PRINT or CAT.
    *Creating RNDMST as a Relative File:
    Ten records with a numeric key of five digits are to be loaded into the Relative File, RNDMST. A Relative File can be easily created and manipulated using The Language C or Pascal, etc. You will implement code to support only the Relative File RNDMST. You will not write an access method to handle Relative Files in general.
    Remember that a Relative File has fixed length cells, each of which is the length of the maximum possible data record. This means you should use a Struct in C and a File of Records in Pascal.
    Conceptually, the 5-digit key of each record may be hashed via truncation of the three high order digits, leaving a 2-digit unique relative record number whose range is 00 to 99. You will pre-allocate 100 cells in RNDMST, one for each potential key. Then you will read each record in RNDSEQ and load it into it's target cell in RNDMST. After the records in RNDSEQ have been loaded into RNDMST, RNDMST will be a sparsely populated Relative File.
    The steps:
    1. Pre-allocate 100 cells in RNDMST.


    for C - define a struct that contains the data fields in RNDSEQ plus a cell status byte.

    - declare an instance of the struct. & write the instance 100 times to disk file, RNDMST.

    2. Load the RNDSEQ records to RNDMST. Then for each record in RNDSEQ:

    - read the record

    - hash the key (truncate the 3 high order digits, leaving a 2-digit number)

    - calculate the offset or relative record number of the hash (2-digit number)

    -- for C, the offset of the target location is hash*sizeof(struct)

    use the seek verb in your language to position the file pointer

    -- for C, fseek ~ for Pascal, seek

    use the read verb in your language to retrieve the record stored at the file pointer

    check the status byte in the retrieved record to ensure that it is "available."

    use the seek verb in your language to reposition the file pointer if the preceding "read" moved it

    -- for C, fseek ~ for Pascal, seek

    use the write verb in your language to write the record to RNDMST

    -- for C, fwrite or ... ~ for Pascal, write

    How can you be sure that you do not write duplicate cells (records



    i still am confused. i thank you in advance if you dare try to help me.
    I Love Jesus

  8. #8
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    howdy,
    i went to university for mechanical engineering and i thought they ask wierd questions, but this thing takes the cake. is this areal CS or IT question??
    WOW

    M.R.

  9. #9
    Unregistered
    Guest
    I agree, the instruction could be clearer, but here's a shot. If it is useful, so be it. If not, oh well.

    Let's say you start out with a data file that is comma delimited like this:

    10012,red,5,20056,blue,21,10892,red,14....

    The pattern is a five digit item ID number followed by a color and then the number of items odered repeated until the end of the file. You can convert this into a sequential file by creating a struct to hold all three fields, reading in enough data from original file to fill all fields of a struct and then writing that struct to the sequential file. Since each struct will have the same number of bytes as any other you can use the fseek() type functions to reposition the filepointer in any location you wish, randomly. This means you can read just the 14th entry from the sequetial file into your program without reading all 100 structs into an array and then searching the array for the information copntained in the 14th struct. You can read the entire entire struct from a sequential file one struct at a time with the read() function if the data was written to the file with the write() function. If you do this, however, the data in the file will (probably) be binary so you can't "tell" what you wrote by looking in the file using a text editor. If you changed the information in the struct you read into the program you can replace it in the file with the updated information using the fseek() functions to position the filepointer and then reading in the entire structs data, even if just one field was changed.

    The next step is to create a new struct type to be used in the relative file. This struct type has all the same fields as the other struct type plus an additional field to indicate whether the spot in the file is available. First you create a file that contains data for 100 of the new structs. Each of the structs in the file initially has default values like 00000 for the id number, clear for the color, 0 for the amount and whatever flag you decide to use for available should indicate that each position is available. Next you populate the file with data from just 10 (in one part of the instructions it says ten and in another it says 100 so you will need to clarifiy this) structs based on the last two digits of the id number (dropping the first three digits from the id number generating a new numbr represents a hash function.) The problem is that unlike a seqeuntial file, the id numbers generated by the hash function may be identical (a process called collision). So you have to account for that. Often you just go to the next available open spot in the array. If you have only 10 of 100 spots "filled" in the file, then you can see why it might be called "sparse".

    What you will probably be doing is finding the offset of the struct to be written to the file. (see instructions for one way to do this) Read the struct data located at that spot into the program. If the data says the spot is open, write the data from the sequential file struct to an instance of the relative file struct, changing the available flag to not available and writing the "filled" relative file struct back to the relative file. If the file position is already occupied then you can look at the next file position, etc., until you find an open file spot and proceed. At no point will there be more than two structs in your program at one time. You will be work with files as much as possible, whcich to me seems part of the learning experience of this assignment.

    IF you don't know about structs then here's a brief overview; but I suggest you look up details in the FAQ/tutorial on this site or in a basic C++ textbook.

    A struct in C++ is a class where every data member is public by default. A struct/class represents a user defined data type that can be used similar to primitive data types such as int, double, char, etc. Here's a possible struct for the relative file if the data was as type as outlined above:

    struct data
    {
    int id;
    char color[10];
    int amount;
    char available;
    }

    I usually declare the above to be global, either by writing it after the #includes and before main() or by creating a separate header file for the struct and #including that. Then in main() I would declare an instance of the struct like this:

    data input;

    and to give it default information I would do this:

    input.id = 00000;
    strcpy(input.color, "clear");
    input.amount = 0;
    input.available = 'y';

    now write this struct to the file 100 times.
    then read the first struct from the sequential file loading it's data into the appropriate field of the struct for the relative file.
    run the hash function to determine where to store the inoformation in the relative file
    read the struct from the relative file at that offset into the program to determine if that cell is available
    if cell is available write the struct data to the file in the appropriate cell/spot
    if cell is not available, check next cell in "sequence" of the relative file, and repeat evaluation until you find an open spot.
    use the relative file to display contents to the screen using whatever relative order you desire, from start to finish, or from end to front being the most logical, but depending on the instructions provided.

    If that isn't enough you might think about how you would retrieve a given set of information randomly from the relative file, including the case where collisions had occurred when writing to the file.

    better sign off before I go to far afield.

Popular pages Recent additions subscribe to a feed