Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ WINAPI中的格式化文件写入_C++_C_Visual Studio 2010_Winapi_Io - Fatal编程技术网

C++ WINAPI中的格式化文件写入

C++ WINAPI中的格式化文件写入,c++,c,visual-studio-2010,winapi,io,C++,C,Visual Studio 2010,Winapi,Io,使用WriteFile命令,我试图以我希望的设计写入文件。问题是我必须使用字符串或缓冲区写入文件 我必须以以下格式打印: a1 a2 a3 a4 a5 b1 b2 b3 b4 b5 . . . 这些是双重类型。我遇到的主要问题是串接字符串和数字。如果这是可能的,我可以简单地做,(假设+是操作符,我们可以:) 和循环,但我没有这个选项 非常感谢您的帮助。将数据预格式化为缓冲区。如果您真的只能使用WinAPI,那么可以使用wsprintf来实现。然后,当一个缓冲区中

使用
WriteFile
命令,我试图以我希望的设计写入文件。问题是我必须使用字符串或缓冲区写入文件

我必须以以下格式打印:

a1   a2   a3   a4   a5
b1   b2   b3   b4   b5
.
.
.
这些是双重类型。我遇到的主要问题是串接字符串和数字。如果这是可能的,我可以简单地做,(假设+是操作符,我们可以:)

和循环,但我没有这个选项


非常感谢您的帮助。

将数据预格式化为缓冲区。如果您真的只能使用WinAPI,那么可以使用
wsprintf
来实现。然后,当一个缓冲区中的数据格式正确时,使用
WriteFile
立即将整个数据写入磁盘。但是,
WriteFile本身根本没有任何格式化或转换功能。

一个选项是使用(或
std::wostringstream
用于宽字符)将数据格式化为字符串,然后将其写入文件

std::ostringstream strm;
double a = 1.1;
double b = 2.2;
strm << "hello " << a << " world " << b;

DWORD written;
WriteFile(handle, strm.str().c_str(), strm.str().size(), &written, NULL);
std::ostringstream strm;
双a=1.1;
双b=2.2;

strm查找std::wstringbuf以满足字符串构造需要。它允许使用与您描述的非常相似的语法。
std::ostringstream strm;
double a = 1.1;
double b = 2.2;
strm << "hello " << a << " world " << b;

DWORD written;
WriteFile(handle, strm.str().c_str(), strm.str().size(), &written, NULL);