0

I'm working on a program that requires me to create a hash table from the contents of a file. The file contains records (1 per line) that include a key (int), name (string), code (int), and a cost (double). I have the code written for most of the program to create the hash table, however, I'm having some trouble figuring out how I should load the table from the file. More specifically, how to store each piece of the record into its corresponding variable?

The code is kind of long, but if you think posting it would help answer my question, let me know and I'll be glad to include it.

I do feel I should include this though, I have a struct set up to hold the information contained in each record that I have set up as follows:

struct record {
        int key;
        string name;
        int code;
        double cost;
}

If you need to see any other portions of the code, or the code as a whole, let me know.

deeiip
  • 3,319
  • 2
  • 22
  • 33
  • what problem are you having, specifically? Do you not know how to use your struct? – Red Alert Dec 02 '14 at 02:56
  • In my main method I'm opening and reading in the file, but I don't know how to store the different pieces of the file into their associated variables. For example, the key should be stored in my int key variable, the name should be in string name, etc. I've never actually had to do this before, the whole saving into specific variables I mean. – HiTechRedneck3 Dec 02 '14 at 02:59
  • The answer you accepted, while it may *appear* complete, has several defects you should know about. Please read [**Why is iostream::eof inside a loop condition considered wrong?**](http://stackoverflow.com/questions/5605125/). Reads may fail at any time and not all fails give you eof status, so this can lead to undefined behavior (or well-defined infinite loops). Making the condition `while(f >> key >> name >> code >> cost)`, as suggested by @RedAlert, and then removing the other reads will fix at least those issues. – HostileFork says dont trust SE Dec 02 '14 at 12:47

2 Answers2

2

You can try this:

while(_fstream >> key && _fstream >> name && _fstream >> code && _fstream >> cost)
{
    record r;
    r.key = key;
    r.name = name; // and so on
    // now you may do whatever you want to with your r object
}

_fstream is your opened file stream for input.


As @Red Alert mentioned, this will be a more elegant solution:

while(_fstream >> key >> name >> code >> cost)

Because _fstream >> key returns the _fstream itself;

deeiip
  • 3,319
  • 2
  • 22
  • 33
0

If the data file has white-space separated items in lines, you can use for example the following code:

#include <iostream>
#include <fstream>

using namespace std;

typedef struct record {
    int key;
    string name;
    int code;
    double cost;
} rec;

int main()
{
    ifstream f ("data.txt");

    rec r;

    f >> r.key;
    while (!f.eof())
    {
            f >> r.name;
            f >> r.code;
            f >> r.cost;

            cout << "new record, key=" << r.key
                    << ", name=" << r.name
                    << ", code=" << r.code
                    << ", cost=" << r.cost << "\n";
            f >> r.key;
    }

    //will also be done by destructor: f.close();

    return 0;
}
Palo
  • 974
  • 12
  • 27
  • You need to test the file *after reading* and *before using*. [See this answer.](http://stackoverflow.com/a/5282311/211160) Also, no need to close an iostream manually, the destructor will do it. – HostileFork says dont trust SE Dec 02 '14 at 03:26
  • yes, it is done exactly so, see the "f >> r.key;" statement. thank you for pointing out the close - but closing early is never bad, if other handling is done in the function. – Palo Dec 02 '14 at 03:31
  • 1
    You're doing several reads...and end of file is not the only potential way that a read can fail. What if the file doesn't exist? What if it's a network drive and it disconnects? What if the data in the file isn't well-formed? While better error handling would certainly be ideal...a solution like in @deeiip's answer at least would not have undefined behavior. The early examples are important. – HostileFork says dont trust SE Dec 02 '14 at 03:39
  • depends what the user needs. if it is a production system, than some exception and error maintenance will have have to come in place anyway. If it is a homework-type project, then I definitely prefer readability, which is pretty low in the other solution. – Palo Dec 02 '14 at 03:59