Boost logo

Boost :

Subject: Re: [boost] [filesystem] wfstream file open does not create file in std::ios_base::out mode
From: Beman Dawes (bdawes_at_[hidden])
Date: 2010-06-23 14:13:16


On Wed, Jun 23, 2010 at 7:20 AM, Beman Dawes <bdawes_at_[hidden]> wrote:
> On Wed, Jun 23, 2010 at 4:27 AM, Ramakrishna, Kirankumar IN BLR SISL
> <Kiran.Ramakrishna_at_[hidden]> wrote:
>> Hi,
>>
>> I upgraded to boost version 1.43, but that did not help. Still file creation fails with wfstream and error code set is ERROR_FILE_NOT_FOUND.
>> Below code captures the error codes
>>
>> #include "boost/filesystem/fstream.hpp"
>> int
>> main()
>> {
>>    boost::filesystem::path   pA("C:\\testA.txt");
>>    boost::filesystem::wpath  pW(L"C:\\testW.txt");
>>    boost::filesystem::fstream fileHandleA;
>>    boost::filesystem::wfstream fileHandleW;
>>    fileHandleA.open(pA, std::ios_base::out);
>>    cout << "fileHandleA.fail() return value is: " << fileHandleA.fail() << endl;
>>    cout << "Error code is: " << GetLastError() << endl;
>>    fileHandleW.open(pW, std::ios_base::out);
>>    cout << "fileHandleW.fail() return value is: " << fileHandleW.fail() << endl;
>>    cout << "Error code is: " << GetLastError() << endl;
>>    return 0;
>> }
>>
>> Am I doing something wrong?
>
> Please, this isn't the right place to post "How do I program in C++?"
> level questions.
>
> The above program won't compile. Beyond the obvious errors, it uses
> poor practices like writing test files to the root directory.
>
> And, once the errors are corrected, it does run OK, at least on the
> trunk. Sorry, but you have exhausted my patience so I didn't try it on
> the actual 1.43 release.

That was a pretty grouchy answer, so let me try again with a bit more
politeness.

Your test program above is overly complex, missing several #includes,
and tries to mix standard library and Win32 error handling, which
don't mix.

Here is a simpler test program that will actually compile:

#include "boost/filesystem/fstream.hpp"
#include <iostream>

int main()
{
   boost::filesystem::ofstream file_a("file_a.txt");
   std::cout << "file_a creation " << (file_a ? "OK" : "failed") << '\n';

   boost::filesystem::wofstream file_w(L"file_w.txt");
   std::cout << "file_w creation " << (file_w ? "OK" : "failed") << '\n';

   return 0;
}

I compiled it against 1.43.0 with VC++ 8 Express like this:

>cl /EHsc /I d:\boost_1_43_0 test.cpp /link /LIBPATH:D:\boost_1_43_0\stage\lib
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

test.cpp
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.

/out:test.exe
/LIBPATH:D:\boost_1_43_0\stage\lib
test.obj

And ran it like this:

>test
file_a creation OK
file_w creation OK
>dir
 Volume in drive C has no label.
 Volume Serial Number is D4A2-EC0D

 Directory of C:\temp

06/23/2010 01:54 PM <DIR> .
06/23/2010 01:54 PM <DIR> ..
06/23/2010 01:54 PM 0 file_a.txt
06/23/2010 01:54 PM 0 file_w.txt
06/23/2010 01:54 PM 360 test.cpp
06/23/2010 01:54 PM 151,040 test.exe
06/23/2010 01:54 PM 274,261 test.obj
...

You might start by trying to compile and run that program yourself.
Once you get it running, then you might try to make a series of very
small changes, compiling and testing at each step of the way, and
building up to larger and more complex programs.

HTH,

--Beman


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk