Portable File Class

Environment: any ANSI C++-compilant compiler.

Overview

A common way of dealing with file I/O in the Windows environment is utilizing MFC's CFile class. But what if you, a professional win32 programmer, are having trouble in programming file operations under Linux, Solaris, or another UNIX system? What if you just don't want to mess with those heavyweight MFC classes?

If your answer is "YES!!!" then the portable 'File' class is a cure for all your diseases.

Advantages

  • The 'File' class utilizes ANSI C++ I/O functions only (I hope.), so it is absolutely portable. There should be no problem in using it on a variety of platforms.
  • Works faster and consumes less memory than MFC's CFile.
  • Class interface is identical to MFC's CFile.
  • Look at the documentation (PortableFileClass.hpp file) for some advanced routines.

Disadvantages

  • The 'File' class is missing routines that deal with file status.
  • Rough changing of file size is not yet implemented.
  • Error handling is not perfect yet...

Usage

  • #include file "PortableFileClass.hpp" to your application's main .cpp file.
  • Declare:

      File myFileObject;

    where myFileObject is a placeholder for the name of your file object.
  • PortableFileClass.hpp file cantains all the required documentation—look at the very beginning of a file.
  • Enjoy.

Good luck!

Downloads

Download demo project - 1.7 Kb
Download source - 3.2 Kb


Comments

  • Buffering

    Posted by Legacy on 08/14/2002 12:00am

    Originally posted by: Gregs

    One of the main reasons to use C functions instead of the C++ streams is the fact the the streams use buffering that can affect the timing of file transactions, especially writing. Buffering can cause the output not to be written if the program crashes, which would affect any logging or debug streams.

    Reply
  • Why???

    Posted by Legacy on 08/13/2002 12:00am

    Originally posted by: musti

    C++ has its own *portable* file input and output stream classes (like fstream, ifstream and ofstream) which are very fast and useful. Why should someone use this class instead of using the built-in classes of C++?

    Reply
  • Reimplementing the wheel...

    Posted by Legacy on 08/13/2002 12:00am

    Originally posted by: Paul A. Howes

    I don't want you to think that your portable CFile class is not good, because it is! You put a lot of work into writing and documenting the class, and it shows.

    However, you should be aware that in C, the standard "fopen", "fclose", etc. are very portable, and in C++, std::fstream and its bretheren are fast, robust, and a part of the ISO C++ standard.

    Reply
  • Why???

    Posted by Legacy on 08/13/2002 12:00am

    Originally posted by: musti

    C++ has its own *portable* file input and output stream classes (like fstream, ifstream and ofstream) which are very fast and useful. Why should someone use this class instead of using the built-in classes of C++?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • When it comes to desktops – physical or virtual – it's all about the applications. Cloud-hosted virtual desktops are growing fast because you get local data center-class security and 24x7 access with the complete personalization and flexibility of your own desktop. Organizations make five common mistakes when it comes to planning and implementing their application management strategy. This eBook tells you what they are and how to avoid them, and offers real-life case studies on customers who didn't …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds