0

在将数据写入文件并关闭文件时,CodeGuard 会产生错误。

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 已传递错误文件或管道流 (0x320D096C)到功能。0x0040AC80 调用 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 错误的文件流 (0x320D096C) 已传递到功能。0x32088358 调用 [通过 0x0040D030] fclose(0x320D096C)

如何解决?

更新-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();

但结果还是一样。

我尝试使用来自http://www.cplusplus.com/reference/ostream/ostream/write/的示例代码

现在 CodeGuard 抛出下一个错误:

ERROR-1: Bad parameter in process: Project1.exe(5908) 错误的文件或管道流 (0x3224096C) 已传递给函数。0x0040DE48 调用 fseek(0x3224096C, 0x0 [0], 0x2 [2])

ERROR-2: Bad parameter in process: Project1.exe(5908) 错误的文件或管道流 (0x3224096C) 已传递给函数。0x0040DE48 调用 fgetpos(0x3224096C, 0x0018F294)

ERROR-3: Bad parameter in process: Project1.exe(5908) 错误的文件或管道流 (0x3224096C) 已传递给函数。0x0040DE48 调用 fsetpos(0x3224096C, 0x0018F28C)

ERROR-4: Bad parameter in process: Project1.exe(5908) 错误的文件或管道流 (0x3224096C) 已传递给函数。0x0040DE48 调用 fgetc(0x3224096C)

ERROR-5: Bad parameter in process: Project1.exe(5908) 错误的文件或管道流 (0x32240984) 已传递给函数。0x0040DE48 调用 fputc(0x78 ['x'], 0x32240984)

ERROR-6: Bad parameter in process: Project1.exe(5908) 错误的文件流 (0x32240984) 已传递给函数。0x321F832C 调用 [通过 0x0041177E] fclose(0x32240984)

我已经在 C++Builder XE7、XE8 和 С++Builder 10.1 Berlin 上测试了这段代码。在所有情况下,CodeGuard 都检测到了这些错误。

4

0 回答 0