简体   繁体   English

c++ 如何使用 fread() 在非 Windows 平台中查找缓冲区长度

[英]c++ How do I find length of buffer in non-windows platforms using fread()

I have a function using ReadFile() where I append a '\0' at the end of the buffer:我有一个 function 使用 ReadFile() 其中我 append 在缓冲区的末尾有一个 '\0' :

HANDLE InFile;
FILE* InputFile;
DWRD len = ftell(InputFile);   
buffer = new BYTE[len + 1];  

if (ReadFile(InFile, buffer , len, &len2, NULL))
{
   ...
   buffer [len2] = 0;      
}

Now I want to adapt this function for use with non-windows platforms.现在我想调整这个 function 以用于非 Windows 平台。 I already changed ReadFile() and use fread() now.我已经更改了 ReadFile() 并现在使用 fread()。 But then I do not have len2 available which I would like to use for appending the '\0':但是我没有 len2 可用,我想用它来附加“\ 0”:

FILE* InputFile;
DWRD len = ftell(InputFile);   
buffer = new BYTE[len + 1];  

if (fread(buffer ,1,len,InputFile)) 
{         
  // how can I find the length of the buffer (len2) now ? 
  buffer [len2] = 0;  // Does not work !       
  ...
}

How can I get the position of the end of my buffer?如何获得缓冲区末尾的 position?

fread() returns the numbers of items actually read, eg: fread()返回实际读取的项目数,例如:

FILE* InputFile;
...
DWORD len = ftell(InputFile);   
buffer = new BYTE[len + 1];  

size_t len2;
if ((len2 = fread(buffer, 1, len, InputFile)) > 0)
{         
  buffer [len2] = 0;
  ...
}
else 
{
    // use feof() and ferror() to check for errors...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM