Access violation reading

0

I need help with resolving this issue. I keep getting this error:

Exception thrown at 0x798F2489 (ucrtbased.dll) in XXXXX.exe: 0xC0000005: Access violation reading location 0x00F30000

The basic logic is that I receive some data from the "server" and write it to the file.

Program stops at this line dValue = ((double)buffer);

for (int j = 0; j < NumberOfPoints; j++) {
        PointName = buffer;
        sprintf_s(FileBuf, "%s: ", PointName);
        // print
        this->printData(file, FileBuf);
        buffer += (strlen(PointName) + 1);

        // check if next byte is int or double
        if (strcmp(PointName, "Level") == 0 || strcmp(PointName, "Acid on output") == 0) {
            iValue = *((int*)buffer);
            sprintf_s(FileBuf, "%d%%\r\n", iValue);
            this->printData(file, FileBuf);
            buffer += 4; // move the point (int size) places
        }
        else {
            dValue = *((double*)buffer); // ---- Error occurs Here!
        }

The whole function is:

int PlantLogger::WriteFile(fstream* file, char* buf, int length){
struct pkg
{
    int length;
    int numberOfChannels;
    char data[1024];
};
pkg* Package = (pkg*)buf;

if (Package->length != length) {
    return 1;
}

char FileBuf[2048];

// Get the current time
time_t CurrentTime;
time(&CurrentTime);
struct tm* MyTime = localtime(&CurrentTime);

// Format the time
char TimeBuf[24];
strftime(TimeBuf, 24, "%d-%m-%Y %H:%M:%S", MyTime);

// Write time to file buffer
sprintf_s(FileBuf, "\r\nMeasurement results at %s\r\n", TimeBuf);

this->printData(file, FileBuf);

char* buffer = Package->data;
char* ChannelName = { 0 };

for (int i = 0; i < Package->numberOfChannels; i++) {
    int NumberOfPoints = *((int*)buffer);
    buffer += 4;
    ChannelName = buffer;
    sprintf_s(FileBuf, "%s:\r\n", ChannelName);
    this->printData(file, FileBuf);
    buffer += (strlen(ChannelName) + 1);

    char* PointName = { 0 };
    int iValue = 0;
    double dValue = 0.0;

    for (int j = 0; j < NumberOfPoints; j++) {
        PointName = buffer;
        sprintf_s(FileBuf, "%s: ", PointName);
        // print
        this->printData(file, FileBuf);
        buffer += (strlen(PointName) + 1);

        // check if next byte is int or double
        if (strcmp(PointName, "Level") == 0 || strcmp(PointName, "Acid on output") == 0) {
            iValue = *((int*)buffer);
            sprintf_s(FileBuf, "%d%%\r\n", iValue);
            this->printData(file, FileBuf);
            buffer += 4; // move the point (int size) places
        }
        else {
            dValue = *((double*)buffer);
        }

        if (strcmp(PointName, "Flow speed") == 0 || strcmp(PointName, "Input solution flow") == 0) {
            sprintf_s(FileBuf, "%.1f\xB0\x43\r\n", dValue);
        }
        if (strcmp(PointName, "Temperature") == 0 || strcmp(PointName, "Output Temperature") == 0) {
            sprintf_s(FileBuf, "%.1fatm\r\n", dValue);
        }
        if (strcmp(PointName, "Pressure") == 0 || strcmp(PointName, "Output pressure") == 0) {
            sprintf_s(FileBuf, "%.1f\r\n", dValue);
        }
        if (strcmp(PointName, "PH") == 0) {
            sprintf_s(FileBuf, "%.1f\r\n", dValue);
        }
        if (strcmp(PointName, "Concentration") == 0) {
            sprintf_s(FileBuf, "%p\r\n", dValue);
        }
        if (strcmp(PointName, "Input flow") == 0) {
            sprintf_s(FileBuf, "%.3fm3/s\r\n", dValue);
        }
        if (strcmp(PointName, "Disulfid on Output") == 0) {
            sprintf_s(FileBuf, "%p/s\r\n", dValue);
        }

        this->printData(file, FileBuf);
        buffer += 8; // move the point (double size) places
    }
}
return 0;}
c++
runtime-error
buffer
access-violation
asked on Stack Overflow Nov 21, 2020 by ArmanK • edited Nov 21, 2020 by ArmanK

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0