1 | #include <iostream>
|
---|
2 |
|
---|
3 | #include <memory>
|
---|
4 |
|
---|
5 | #include <boost/filesystem.hpp>
|
---|
6 | #include <boost/filesystem/fstream.hpp>
|
---|
7 |
|
---|
8 | #include <windows.h>
|
---|
9 |
|
---|
10 | void explain (std::string what) {
|
---|
11 | DWORD e = GetLastError ();
|
---|
12 |
|
---|
13 | std::cerr << what << ": ";
|
---|
14 |
|
---|
15 | DWORD r;
|
---|
16 | std::shared_ptr<char> buf;
|
---|
17 | {
|
---|
18 | LPSTR buf_;
|
---|
19 | r = FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER
|
---|
20 | | FORMAT_MESSAGE_FROM_SYSTEM
|
---|
21 | | FORMAT_MESSAGE_IGNORE_INSERTS,
|
---|
22 | 0,
|
---|
23 | e,
|
---|
24 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
---|
25 | reinterpret_cast<LPSTR> (&buf_), 0,
|
---|
26 | 0);
|
---|
27 | buf.reset (buf_, LocalFree);
|
---|
28 | }
|
---|
29 |
|
---|
30 | if (r) {
|
---|
31 | char* end = buf.get () + std::strlen (buf.get ());
|
---|
32 | if (std::distance (buf.get (), end) >= 2 && *(end-2) == '\r' && *(end-1) == '\n')
|
---|
33 | end -= 2;
|
---|
34 | std::copy (buf.get (), end, std::ostream_iterator<char> (std::cerr));
|
---|
35 | }
|
---|
36 | else
|
---|
37 | std::cerr << "?[" << GetLastError () << "]";
|
---|
38 |
|
---|
39 | std::cerr << " (" << e << ")\n";
|
---|
40 | }
|
---|
41 |
|
---|
42 | int main () {
|
---|
43 | using namespace boost::filesystem;
|
---|
44 |
|
---|
45 | path p = L"umbre☂☂a";
|
---|
46 |
|
---|
47 | HANDLE fh = CreateFileW (p.c_str (), GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, 0);
|
---|
48 | if (fh == INVALID_HANDLE_VALUE)
|
---|
49 | explain ("CreateFileW");
|
---|
50 | else {
|
---|
51 | if (!CloseHandle (fh)) {
|
---|
52 | explain ("CloseHandle");
|
---|
53 | return 1;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | ofstream f (p, ofstream::out);
|
---|
58 | if (!f) {
|
---|
59 | explain ("ofstream");
|
---|
60 | return 1;
|
---|
61 | }
|
---|
62 |
|
---|
63 | f << "blah blah\n";
|
---|
64 | f.close ();
|
---|
65 | if (!f) {
|
---|
66 | explain ("close");
|
---|
67 | return 1;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | // vim: ts=4 sw=4 noet
|
---|