2

I'm supposed to read in a data file with fixed length records and fields, create a sorted index list in memory, and save that list to a file. Then I'm to write a second program that interactively (via the Linux command line) takes a key and the index file name, opens and loads the index file, searches for the given key using the index table, and opens and returns the correct data record.

The original file consists of a list of records with a key (int), a name (string of 8 characters max), a code (int) and a cost (double).

The RRN (relative record number) begins at 1, with the RRN at 0 representing a dummy record with only the size in the first entry.

Here is the data file I will be using.

8 blank 0 0.0
12345 Item06 45 14.2
12434 Item04 21 17.3
12382 Item09 62 41.37
34186 Item25 18 17.75
12165 Item16 30 7.69
16541 Item12 21 9.99
21212 Itme31 19 8.35
41742 Item14 55 12.36

The execution is supposed to work in the following manner from the command line in Linux:

search 12382 prog5.idx

with prog5.idx being the index file created.

I have some code written, but so far all it does is open the data file.

#include <iostream>
#include <fstream>

using namespace std;

int main() {
    ifstream data;
    data.open("prog5.dat");
    ofstream outFile("prog5.idx", ios::out);

    //if file can't be opened, exit
    if(!data) {
        cerr << "Open Failure" << endl;
        exit(1);
    }
    else {
        cout << "File is open" << endl;
    }
}

The "File is Open" part will be replaced once I figure out what do do once the file is open, just used this message to verify that it was opening the file.

I've never worked with these kinds of files before so have no idea where to go from here.

  • Is a school assignment? Can you use the containers in the C++ standard library? In case, you might use `std:map` with `std:pair` where `Record` is a simple `struct` with the information in a line.. – gmas80 Oct 27 '14 at 23:05
  • I believe so, I have 0 experience with the C++ libraries though. –  Oct 27 '14 at 23:06

1 Answers1

0

I will provide you with a possible naive draft for the first program, so that you can understand the general idea:

#include <map>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

struct Record
{
    int key;
    char name[8];
    int code;
    double cost;
    size_t offset;
};

int main() {
    std::map<int,Record> mymap;
    ifstream data;
    size_t offset_count = 0;
    data.open("prog5.dat");
    ofstream outFile("prog5.idx", ios::out);

    //if file can't be opened, exit
    if(!data) {
        cerr << "Open Failure" << endl;
        exit(1);
    }
    else {
        cout << "File is open" << endl;
    }

    std::string line;
    while (std::getline(data, line))
    {
        std::istringstream iss(line);
        Record tmp;
        if (!(iss >> tmp.key >> tmp.name >> tmp.code >> tmp.cost))
        { 
            break; // error, do something
        }
        tmp.offset = offset_count;
        offset_count += sizeof(Record); 
        mymap.insert( pair<int,Record>(tmp.key,tmp) );
    }

    // Now you have all the info (and much more) you need in memory,
    // thus serialize the key and its position on a file 
    // So you have done the first part of the assignment

}

Look at the example here if you don't know how to iterate within a std:map.

gmas80
  • 1,218
  • 1
  • 14
  • 44
  • This looks great, thanks a bunch. One question, "// thus serialize the key and its position on a file", how would I do that? –  Oct 27 '14 at 23:46
  • 1
    You can decide to write the index information in a text or in a binary file.. This changes how then you have to recover the file index content. Useful SO answers: http://stackoverflow.com/a/12935205/2741329 and http://stackoverflow.com/a/14413863/2741329 – gmas80 Oct 27 '14 at 23:55