Featured

    Featured Posts

File Handling Mechanism

File Handling Mechanism :->

  • File handling in VC++ is done with the help of CArchive class and CFile class.
  • Open(), close(), write() are its member function.
  • CFile constructor will automatically open the file.
  • CFile destructor will automatically close the file when object goes out of a scope.
  • For Write file, we will use the view classes function onFilewrite().
  • For Read File we will use the view class function OnFileRead().
There are two ways to Access the file :
-> Sequential Access.
-> Random Access.
  1.  Sequential Access :->

    In sequential access, from beginning to end and read it in the same way.  It is used only for text files.
    void CFile view : : OnFileWrite()
    {
    Cstring.hello_string = " Hello ";
    Cfile.Filename ( " Hello.txt ", CFile : : mode Read );
    Filename.write ( hello string; hell_string.Get length );
    Filename.Close();
    }

    2. Random Access :->

    In this, file are divided into records so that we know exactly where the data is and we can access randomly seek() function of CFile class is used here.
    void CFileview : : On File Write()
    {
    Const MAX_LEN = 20;
    Const MAX_items = 4;
    Cstring hell_string = " Hello world";
    CFile.Filename( "hello_txt", CFile : : mode write );
    Filename.write( hello_string, hello string);
    Filename.close();
    strcpy( output_string[0], "This");
    strcpy( output_string[1],"is");
    strcpy( output_string[2], "a");
    strcpy( output_string[3], "txt");
    CFile random_file( data.txt, CFile : : mode write );
    For ( int i = 0; 1<MAX_item; i++)
  2. { random_File.write Coutputstring[1]; }
    random_File.close();

File Handling Routines :->

  • Chsize  - size of the file.
  • File length - Get the length of file
Set mode - Set the mode of file.
File handling is done in VC++ with the help of two classes :
  • CArchive class
  • CFile class.




CArchive class : 

It is used in single document or multiple document application.  For file handling CArchive class is an engine that drives the serialization mechanism or file handling in SDI & MDI application.  The object of CArchive class used for streaming of object out to a file or removing them again as an input stream.  A CArchive object has a CFile within it which provides this I/P and o/p capacity.  File binary files and provides the actual connection to the physical file with in the serialization process, the CFile object takes care of all the files i/p operation.
Void CMyDoc : : Serialize( CArchive &ar )
{
if(ar.Isstring () )
 ar << st ;
}
else
{
ar >> st ;
}

 CFile :->

   Object of CFile provides the input and output capability.  For binary files and provides the actual connection to the physical file.  Object of Cfile class is generally used in dialog based application.  The members Functions are same as in file. Class of C++ and has the same mechanism of storing data.

Post a Comment

www.CodeNirvana.in

www.posthatke.com. Powered by Blogger.

Google+ Followers

Copyright © www.posthatke.com | Blogger Templates | Designed By posthatke.com