0

此处的代码块运行良好,直到调用 _read 函数,之后它无缘无故地更改文件句柄变量 'fh' 的值。

    std::string& xLogFile;
    std::string& xBuffer;
    struct _stat& xStatBuffer)

char *buffer;
buffer = (char *)malloc(sizeof(char) * xStatBuffer.st_size);
#define _O_RDONLY       0x0000  /* open for reading only */

int fh = 0, read_bytes =0;
fh = _open(xLogFile.c_str(), _O_RDONLY);  // ToDo function deprecated should be changed to fstream

if (fh ==1)
{
    if (mWriteLog) IntPkgUtil::TraceLog("Error!! Couldn't open the log file");
    return true;
}

read_bytes = _read(fh,&buffer,xStatBuffer.st_size);

_close(fh);
if (read_bytes <= 0)
{
    if (mWriteLog) IntPkgUtil::TraceLog("Error!! Couldn't read the log file");
    return true;
}
buffer[read_bytes] = '\0';
xBuffer = buffer;

这是我用来从文件读取到缓冲区的代码块,但它在 _read 函数处失败,其中文件句柄“fh”的值在调用函数后发生变化。

4

1 回答 1

1

修复代码如下,缓冲区而不是 &buffer。您正在覆盖堆栈。

read_bytes = _read(fh,buffer,xStatBuffer.st_size);
于 2013-09-12T07:10:20.490 回答