Hi, Im trying to figure out(again) the best(fastest) way to handle file management..Im using Win32 api, so I always try to use the api routines(instead of the c++ ones), dont know if this is better, but I just feel it is better in this way..
( On "windows internals" book, the author says the c++ open routines in fact call window createfile routine, so using it direct makes more sense to me...but Im stil noob and maybe this is crap )
But the win32 api just have open, read and write operations( and set file pointer), while fstream have lots of specific routines...

Theres a best approach?
Should I do I/O direct to the file or should I load the entire file on an array, and manipulate the array?
The approach changes for ascii files?

I always get stucked on files..its too much little details you have to handle to manage a specific file type( skip comments, check what kind of info have in position 'x', get the info( formating )..The problem is that the file type specification are not constants, you cant just load it on your struct, because the bastard can have comments in any place, or have lots of different organization..(what a pin in the arse..)

My begining code to manage the file :

OPENFILENAME openFile = {0};

					openFile.lStructSize = sizeof(OPENFILENAME);
					openFile.hwndOwner = hWnd;

						TCHAR szFilters[] = { TEXT("Any OBJ file\0*.OBJ;*.obj;*.Obj\0\0") };
					openFile.lpstrFilter = szFilters;

						TCHAR szFileName[256] = { TEXT("\0") };
					openFile.lpstrFile = szFileName;
					openFile.nMaxFile = 256;

						TCHAR szFileTitle[256] = { TEXT("\0") };
					openFile.lpstrFileTitle = szFileTitle;
					openFile.nMaxFileTitle = 256;

						TCHAR *szDir = _getcwd( NULL, 0 );
					openFile.lpstrInitialDir = szDir;

					openFile.Flags =  dwOpenFlags;

						TCHAR szExt[] = { TEXT("obj") };
					openFile.lpstrDefExt = szExt;

					if( GetOpenFileName( &openFile ) ){

						HANDLE hFile = CreateFile( openFile.lpstrFile, GENERIC_READ, FILE_SHARE_READ,

						LARGE_INTEGER fsize = {0};


						if( hFile != INVALID_HANDLE_VALUE ){

							DWORD dwBytesRead;
								GetFileSizeEx( hFile, &fsize );
								myOBJ.pBufferData = new BYTE [fsize.QuadPart];

							if( ReadFile( hFile, myOBJ.pBufferData, fsize.QuadPart, &dwBytesRead, NULL ) ){

							//hardcore stuff goes here



						CloseHandle( hFile );

					delete [] szDir;

The best approach is to use standard c++ file i/o classes and methods because that gives your program the best portability should you decide to port it to another operating system sometime in the future. Using win32 api file i/o functions is a little like limiting yourself to only binary reads/writes because they know nothing at all about text files.

This article has been dead for over six months. Start a new discussion instead.