Access violation reading location using binary file
<p>First off, I know there are posts with similar problems, but I cannot find the solution to mine in any of them. </p>

<p>This is for a programming assignment using binary and text files to store "Corporate sales data." (Division name, quarter, and sales), and then to search for specified records inside the binary data file and display them.</p>

<p>Here are the important parts of my code:</p>

<pre><code>#include stuff
...

// Struct to hold division data
struct DIVISION_DATA_S
{
    string divisionName;
    int quarter;
    double sales;
};

int main()
{
    ...

    // Open the data file
    fstream dataFile;
    dataFile.open(dataFilePath, ios::in | ios::out | ios::binary);

    ... Get data from user, store in an instance of my struct ...

    // Dump struct into binary file
    dataFile.write(reinterpret_cast&lt;char *&gt;(&amp;divisionData), sizeof(divisionData));        

    // Cycle through the targets file and display the record from divisiondata.dat for each entry
while(targetsFile &gt;&gt; targetDivisionName)
{       
    int targetQuarter;  // Target quarter
    string targetQuarterStr;
    targetsFile.ignore();   // Ignore the residual '\n' from the "&gt;&gt;" read
    getline(targetsFile, targetQuarterStr);
    targetQuarter = atoi(targetQuarterStr.c_str()); // Parses into an int

    cout &lt;&lt; "Target: " &lt;&lt; targetDivisionName &lt;&lt; " " &lt;&lt; targetQuarter &lt;&lt; endl;

    // Linear search the data file for the required name and quarter to find sales amount
    double salesOfTarget;
    bool isFound = false;
    while (!isFound &amp;&amp; !dataFile.eof())
    {
        cout &lt;&lt; "Found division data: " &lt;&lt; targetDivisionName &lt;&lt; " " &lt;&lt; targetQuarter &lt;&lt; endl;
        DIVISION_DATA_S divisionData;

        // Read an object from the file, cast as DIVISION_DATA_S
        dataFile.read(reinterpret_cast&lt;char *&gt;(&amp;divisionData), sizeof(divisionData));
        cout &lt;&lt; "Successfully read data for " &lt;&lt; targetDivisionName &lt;&lt; " " &lt;&lt; targetQuarter &lt;&lt; endl
            &lt;&lt; "Name: " &lt;&lt; divisionData.divisionName &lt;&lt; ", Q: " &lt;&lt; divisionData.quarter &lt;&lt; ", "
            &lt;&lt; "Sales: " &lt;&lt; divisionData.sales &lt;&lt; endl;

        // Test for a match of both fields
        if (divisionData.divisionName == targetDivisionName &amp;&amp; divisionData.quarter == targetQuarter)
        {
            isFound = true;
            cout &lt;&lt; "Match!" &lt;&lt; endl;
            salesOfTarget = divisionData.sales;
        }
    }
    if (!isFound)   // Error message if record is not found in data file
    {
        cout &lt;&lt; "\nError. Could not find record for " &lt;&lt; targetDivisionName
            &lt;&lt; " division, quarter " &lt;&lt; targetQuarter &lt;&lt; endl;
    }
    else
    {
        // Display the corresponding record
        cout &lt;&lt; "Division: " &lt;&lt; targetDivisionName &lt;&lt; ", Quarter: " &lt;&lt; targetQuarter
            &lt;&lt; "Sales: " &lt;&lt; salesOfTarget &lt;&lt; endl;
        totalSales += salesOfTarget;    // Add current sales to the sales accumulator
        numberOfSalesFound++;   // Increment total number of sales found
    }
}
</code></pre>

<p>Sorry for the lack of indent for the while loop, copy/paste kind of messed it up. </p>

<p>My problem appears when attempting to access information read from the binary file. For instance, when it tries to execute the <code>cout</code> statement I added for debugging, it gives me this error: </p>

<pre><code>Unhandled exception at 0x0FED70B6 (msvcp140d.dll) in CorporateSalesData.exe: 0xC0000005: Access violation reading location 0x310A0D68.
</code></pre>

<p>Now, from what I have read, it seems that this means something is trying to read from the very low regions of memory, AKA something somewhere has something to do with a null pointer, but I can't imagine how that would appear. This whole read operation is copied exactly from my textbook, and I have no idea what a <code>reinterpret_chast</code> is, let alone how it works or how to fix errors with it. Please help?</p>

<p>EDIT: Thanks for all the help. To avoid complications or using something I don't fully understand, I'm gonna go with switching to a c-string for the divisionName.</p>