0

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.

Ig_M
  • 107
  • 9
  • 1
    Are you sure that the file is actually opened? And if you provide flags of any kind when opening the file, you actually need to add the open-mode as well (`in` or `out`). – Some programmer dude Dec 25 '18 at 20:43
  • Yes, I am sure. Data was written to file. Changing to: ofstream myfile("d:/output.txt", std::ofstream::binary | std::ofstream::out); didn't help. – Ig_M Dec 25 '18 at 22:12
  • Unrelated to your problem, but what use is the `buffer` pointer and the memory you allocate dynamically? Why not simply write out the `data` array directly? – Some programmer dude Dec 25 '18 at 22:22
  • It is only to simulate filling the buffer with data. But even if write out the data array directly, CodeGuard still generates these 2 errors. If you have installed the C++Builder, please check this code with CodeGuard enabled. – Ig_M Dec 25 '18 at 22:31
  • @Someprogrammerdude `ofstream` sets the `out` flag automatically, no need to set it manually. – Remy Lebeau Dec 28 '18 at 01:38
  • @IgorMarchenko the code is fine, except that you are not checking if the file is actually open before you write to it. Apparently that is failing, that is what the error is complaining about. For instance, you might not have write access to the root of `d:`, so the file fails to be created. You need to add a check for open failure before calling `write()`. – Remy Lebeau Dec 28 '18 at 01:39
  • @RemyLebeau It's set as the default argument for the mode in the constructor. But is it specified that the `std::ofstream` constructor always will set it even if the default argument is overridden (as in the OP's example)? – Some programmer dude Dec 28 '18 at 11:20
  • @Someprogrammerdude yes, it is. `ofstream` OR's the `out` flag with whatever the caller passes in. Same with `ifstream` and the `in` flag – Remy Lebeau Dec 28 '18 at 18:00
  • @RemyLebeau, see upd-1. It is look like CodeGuard's bug. – Ig_M Dec 29 '18 at 13:17
  • 1
    @IgorMarchenko doesn't surprise me. CodeGuard is notoriously buggy, I stopped using it years ago – Remy Lebeau Dec 29 '18 at 17:43
  • @RemyLebeau, OK. Is there an alternative tool for C++ Builder to replace the CodeGuard? – Ig_M Dec 29 '18 at 19:14

0 Answers0