Access violation reading location 0x00000014. Using Streams

0

So I have this ifStream and it reads alright from the file. I read one line in order to check the type of item I am reading in. Then After I have read in that i pass the stream to another class which reads some more but when it gets to this class it throws this exception. Access violation reading location 0x00000014. Here is the code where it shows this error.

void Author::readData(ifstream & stream1)
{
    string line = "";
    getline(stream1, name); //The error happens here
    if (stream1.fail() && stream1.eof())
    {
        throw Error(-1);
    }
    else if (stream1.fail() && !(stream1.eof()))
    {
        throw Error(0);
    }
    getline(stream1, address);
    if (stream1.fail() && stream1.eof())
    {
        throw Error(-1);
    }
    else if (stream1.fail() && !(stream1.eof()))
    {
        throw Error(0);
    }
}

void Book::readData(ifstream & stream1)
{   

    (*theAuthor).readData(stream1); //This is where the function is called. Let me check to see if It is even initialized here.
    if (stream1.fail() && stream1.eof())
    {
        throw Error(-1);
    }
    else if (stream1.fail() && !(stream1.eof()))
    {
        throw Error(0);
    }
    getline(stream1, title);
    if (stream1.fail() && stream1.eof())
    {
        throw Error(-1);
    }
    else if (stream1.fail() && !(stream1.eof()))
    {
        throw Error(0);
    }

I have done research on many differing websites but I cant seem to find any that explain why an ifstream is throwing this exception. Please help.

Edit: Added some more code in hopes it may help.

c++
ifstream
asked on Stack Overflow Jul 30, 2016 by Mindstormer • edited Jul 30, 2016 by Mindstormer

1 Answer

1

There's nothing wrong with the shown code.

What all those websites you've read have not fully explained to you is just because a C++ program crashes on a particular line does not mean that's where the bug is. I can easily come up with a minimum example that strcpy()s past the end of the array, corrupting the stack, then proceeds to do a bunch of work, and only crashing when an attempt is made to return from the function. There's nothing wrong with the return statement in C++, yet the code will crash at that point.

And there's nothing wrong with your getline() call either, or how it's used.

A programming error or a bug typically results in corrupted memory, but after stomping all over the memory, the code might not immediately attempt to use it, in any meaningful way, but proceeds on its way. At some pointer later, the program goes back and attempts to use its data structures, encounters garbage, and crashes.

The most likely explanation is that a bug that happened earlier ended up scribbling all over your std::ifstream, and/or name std::string. Now, you get around to calling getline() to read from the stream into this string, and the code blows up. Or, perhaps, something screwed up a pointer to an instance of this Author class. An attempt is made to execute this class method. The code blows up because the name class member is, obviously, utter junk. The bug would not be here, of course, but wherever the pointer to this Author class got screwed up.

The only way you can get help with your problem would be if you post a minimum, complete, and verifiable example that anyone can compile, execute, and reproduce your bug (pay attention to the "minimum" requirement, that doesn't mean that you get to post your entire code).

You will need to investigate the rest of your entire program, looking for your bug. Welcome to C++.

answered on Stack Overflow Jul 30, 2016 by Sam Varshavchik • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0