Writing data to file and CodeGuard error notifications

180 Views Asked by At

While writing data to the file and closing the file, CodeGuard generates errors.

char *buffer = new char[10];
char data[] = "abcdefghij";
memcpy(&buffer[0], &data[0], 10);

//create file
ofstream myfile("d:/output.txt", std::ofstream::binary);

//write data
myfile.write(&buffer[0], 10);//at this line the CodeGuard throws an ERROR-1

delete []buffer;

myfile.close();//at this line the CodeGuard throws an ERROR-2

ERROR-1: Bad parameter in process: Project1.exe(3916) - c:\program files (x86)\embarcadero\studio\15.0\include\dinkumware\fstream#246 A bad file or pipe stream (0x320D096C) has been passed to the function. 0x0040AC80 Call to fputc(0x61 ['a'], 0x320D096C)

ERROR-2: Bad parameter in process: Project1.exe(3916) - c:\program files (x86)\embarcadero\studio\15.0\include\dinkumware\fstream#180 A bad file stream (0x320D096C) has been passed to the function. 0x32088358 Call to [via 0x0040D030] fclose(0x320D096C)

How to fix it?

Upd-1:

ofstream myfile("d:/output.txt", std::ofstream::binary | std::ofstream::out);
char buffer[] = "abcdefghij";

    // 
    if(myfile.is_open())
    {
    myfile.write(reinterpret_cast<char*>(&buffer[0]), 11);//+1 for \0
    }

myfile.close();

But result is still the same.

I tried to use sample code from http://www.cplusplus.com/reference/ostream/ostream/write/

And now CodeGuard throws next errors:

ERROR-1: Bad parameter in process: Project1.exe(5908) A bad file or pipe stream (0x3224096C) has been passed to the function. 0x0040DE48 Call to fseek(0x3224096C, 0x0 [0], 0x2 [2])

ERROR-2: Bad parameter in process: Project1.exe(5908) A bad file or pipe stream (0x3224096C) has been passed to the function. 0x0040DE48 Call to fgetpos(0x3224096C, 0x0018F294)

ERROR-3: Bad parameter in process: Project1.exe(5908) A bad file or pipe stream (0x3224096C) has been passed to the function. 0x0040DE48 Call to fsetpos(0x3224096C, 0x0018F28C)

ERROR-4: Bad parameter in process: Project1.exe(5908) A bad file or pipe stream (0x3224096C) has been passed to the function. 0x0040DE48 Call to fgetc(0x3224096C)

ERROR-5: Bad parameter in process: Project1.exe(5908) A bad file or pipe stream (0x32240984) has been passed to the function. 0x0040DE48 Call to fputc(0x78 ['x'], 0x32240984)

ERROR-6: Bad parameter in process: Project1.exe(5908) A bad file stream (0x32240984) has been passed to the function. 0x321F832C Call to [via 0x0041177E] fclose(0x32240984)

I have tested this code on C++Builder XE7, XE8 and С++Builder 10.1 Berlin. And in all cases CodeGuard detected these errors.

0

There are 0 best solutions below