I am building applications targeting Windows Mobile, I need to read and write files. I am not sure whether to use the C++ standard library (iostreams) or the Windows API (ReadFile(), WriteFile()...). I think the Windows API is faster than the C++ standard library, but they are not object oriented, I have to encapsulate all the IO functions in a class.

Which choice do you think is better?

Kevin Tse

If your target os supports standard iostreams then I'd use it. win32 api is very low level and supports only binary files -- yes text files can be written as binary files but using std::fstream makes it a lot easier.

Using a standard library may allow your software to be cross-platform or easier to port to another platform. I'd say stick with the standard library, it'll be much easier and keep everything OO.

commented: agree. +25

As others have pointed out every possibility, I'll have to add that you might as well consider using embedded visual c++ and MFC.

Cheers,
Rajesh R Subramanian
[CodeProject MVP]
[Microsoft MVP]

I spend about 50% to 60% of my time doing Windows CE and I just use the stdio.h functions for text output and I usually do the Api functions (CreateFile, SetFilePointer, etc.) for binary access. Here's an example of opening my DEBUG output file in one of my CE programs...

FILE* fp;

#if defined(MYDEBUG)
 fp=_tfopen(_T("\\C_DRIVE\\Silvah\\Output.txt"),_T("w"));
 _ftprintf(fp,_T("Output.txt Opened During fnWndProc_OnCreate()\n"));
 _ftprintf(fp,_T("MYDEBUG Is Defined!\n"));
 #endif

Thanks for all your replies and suggestions!
I do also have to handle binary files, and files with their names and contents encoded in Unicode, and sometimes I have to read certain count of bytes, I can handle this easily with the Windows API. but I am going to stick with the C++ standard library as possible as can be.
Well I am stilling learning C++, I am not so sure whether the C++ iostreams can do all that the Windows API can.

OK, I will mark this thread as solved, I think I have to find another half of the answer myself. Thank you!

>>I am not so sure whether the C++ iostreams can do all that the Windows API can.

If your compiler supports iostreams for the os, then the answer is yes. For binary data just call iostream's read() and write() methods, which are nearly identical to win32 api ReadFile() and WriteFile() functions. The win32 api functions allow for gigantic huge files, but you don't need to worry about that because you can not create/store files that big on WinCE devices.

In terms of files, with the exception of the unicode situation, there doesn't seem to be much difference between desktop Windows and Windows CE. I don't do iostream, but I'd be very surprised if it didn't work identically on CE to its behavior on desktop Windows. In terms of anything in stdio or the Windows Api itself, I've never encountered any differences except some fairly well know things such as lack of a default path, unicode, etc. What differences you'll find on CE compared to desktop Windows will be on other aspects than the file system.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.