Question

I have a simple function, which writes to a .txt file. This method works perfectly in a simple C++ project. But the same function, when created in a C++ SmartDeviceProject, does not write anything to the file.

What is the problem?

The code sample is given below.

STDMETHODIMP CHelloproxy::Hai(void)
{
    CTime t = CTime::GetCurrentTime();

    fstream file("D:\\k.txt", ios::app);

    file << t.GetHour() << ":" << t.GetMinute() << ":" << t.GetSecond() << "-"
         << "Add() operation started..." << endl;

    //file<<t.Format() << "-" << "Add() operation started..." << endl;

    HRESULT hr = proxy->Hai();

    file <<t.GetHour() << ":" << t.GetMinute() << ":" << t.GetSecond() 
         << "-" << "Add() operation finished..." << endl;

    file.close();

    return hr;
}
Was it helpful?

Solution

Please check the validity of the line fstream file("D:\k.txt", ios::app); mean "May be d:\ is not existing in smartphone".

Following are the details fstream object creation

http://www.cplusplus.com/reference/iostream/fstream/fstream/

" fstream ( ); explicit fstream ( const char * filename, ios_base

:openmode mode = ios_base::in | ios_base::out );

Construct object and optionally open file Constructs an object of the fstream class. This implies the initialization of the associated filebuf object and the call to the constructor of its base class with the filebuf object as parameter.

Additionally, when the second constructor version is used, the stream is associated with a physical file as if a call to the member function open with the same parameters was made.

If the constructor is not successful in opening the file, the object is still created although no file is associated to the stream buffer and the stream's failbit is set (which can be checked with inherited member fail). "

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top