1

So I've got this fstream that I'm reading from my file with, and I get this insane bug when trying to read from my file after the EOF flag is set (or at least that's what I think is happening).

This is the scope of my problem:

if (!reader.good())
        {
            reader.clear();
            reader.seekg(0, ios::beg);
        }
int test = reader.tellg();
reader.seekg((index / 10) * 10 * sizeof(TrieNode));
test = reader.tellg();
reader.read((char*)TrieBuff, 10 * sizeof(TrieNode));
test = reader.tellg();

I've added the integer 'test' so that I could track the position of the stream in the debugger for every step of the way. Now what I've been seeing in the debugger is that even when the program enters the 'if' and clears the fstream, test still equals -1 afterwards! And then nothing is read into TrieBuff. I have no idea what to do here.

Here's the entire function just incase it's relevant:

// Takes an fstream opened at the doc's trie file. Returns the node at the 
    // index specified, using the TrieBuff. (The index is the node's serial number.)
    TrieNode get_node_at_index(fstream& reader, int index)
    {
        if (TrieBuff[0].data.nodeserialnr == -1 ||
            index > TrieBuff[9].data.nodeserialnr || index < TrieBuff[0].data.nodeserialnr)
        {
            if (!reader.good())
            {
                reader.clear();
                reader.seekg(0, ios::beg);
            }
            int test = reader.tellg();
            reader.seekg((index / 10) * 10 * sizeof(TrieNode));
            test = reader.tellg();
            reader.read((char*)TrieBuff, 10 * sizeof(TrieNode));
            test = reader.tellg();
            int x = 0;
        }
        return TrieBuff[index % 10];
    }
Scott Steinbach
  • 307
  • 1
  • 5
  • Did you open the file in text mode or binary mode? – PaulMcKenzie Jun 11 '15 at 09:37
  • @ScottSteinbach Please add the line `std::cout << (reader.is_open() ? "open\n" : "closed\n");` as the first line of `get_node_at_index` and tell us what it prints just before you find that `int test = reader.tellg();` sets `test` == -1. – Mike Kinghan Jun 12 '15 at 16:49

2 Answers2

1

I couldn't understand the use of (index / 10) * 10 * sizeof(TrieNode). It it is used purposefully, then please check that it will be evaluated to 0 for the index, which is not divisible by 10. Since index / 10 is an integer division. I guess due to that read pointer is not moving forward.

kravi
  • 747
  • 1
  • 8
  • 13
  • I do use it purposefully and its purpose is that I interact with my trie file using blocks of ten. so if I wanted to access the 5th node I would read the 0th block, and if I wanted the 42nd node I would read the 4th block. So the integer division is intended. But regardless even when the function enters the if statement, when 'int test = reader.tellg()' is called, the debugger shows test equals -1. Why isn't 'seekg(0, ios::beg)' resetting it to 0? – Scott Steinbach Jun 11 '15 at 10:00
  • @ScottSteinbach I think you missed kravi's point, if you divide something my 10 and do it times 10 the value doesn't change. Also you give yourself the risk of getting a devided by zero exception since you cant divide 0. So an improved version of that line would be: index * sizeof(TrieNode) It gives the same value as (index / 10) * 10 * sizeof(TrieNode) – Thealon Jun 11 '15 at 10:10
  • @Thealon: _"if you divide something my 10 and do it times 10 the value doesn't change"_ That's not true. You're forgetting about integer division. I think you missed kravi's point. – Lightness Races in Orbit Jun 20 '15 at 13:40
0

So the answer was, as some suggested, that the fstream was not opening correctly. I hadn't realized that stream.good() will return true even if the stream is not opened. Thanks everyone for contributing.

Scott Steinbach
  • 307
  • 1
  • 5