using sprintf instead of fprintf

sprintf returns the bytes length that it writes, so you can:

  1. char buffer[1024];
  2. size_t pos=(size_t)buffer;
  3. pos+=sprintf((char*)pos,"contents here\n");
  4. pos+=sprintf((char*)pos,"second line.");
  5. printf("%s",buffer);
  6. //output:
  7. //contents here
  8. //second line.

if you in linux, there is many ways to do this.

but how to map byte array in windows?

mem file map?
https://docs.microsoft.com/en-us/dotnet/standard/io/memory-mapped-files

How make FILE* from HANDLE in WinApi?

You can do this but you have to do it in two steps. First, call _open_osfhandle() to get a C run-time file descriptor from a Win32 HANDLE value, then call _fdopen() to get a FILE* object from the file descriptor.

  • 这些函数是否拥有基础句柄的所有权,还是应该仍然调用CloseHandle? – user877329 2014年 11月13日在9:11
  • @ user877329:我认为您仍然需要致电CloseHandle(),但是值得检查。自从我使用这些功能已经有一段时间了。编译器随附了Microsoft运行时库源代码,因此您可以编写测试程序并对其进行跟踪以进行检查。