First:
Header:
<Fstream>
Namespace:
STD
The member function open () is used to open a file.
Prototype: void open (const char * filename, int mode, int access );
Where: Filename: name of the file to be opened
Mode: how to open the file
Access: the default attribute for opening a file is opening protection.
How to open a file:
IOS: APP: open a file in append Mode
IOS: ate: After the file is opened, it is located at the end of the file.
IOS: Binary: open a file in binary mode. The default mode is text.
IOS: In: open the file as input
IOS: Out: open an output file
IOS: trunc: if the file exists, set the file length to 0.
Fstream FS ("fstream.txt", IOS: In | IOs: Out | IOs: trunc); <br/> If (! FS. bad () <br/>{< br/> // write to the file. <br/> FS <"writing to a basic_fstream object... "<Endl; <br/> FS. close (); <br/> // dump the contents of the file to cout. <br/> FS. open ("fstream.txt", IOS: In); <br/> cout <FS. rdbuf (); <br/> // FS> buffer; <br/> FS. close (); <br/>}
Fstream has two subclasses: ifstream (input file stream) and ofstream (output file stream). ifstream opens files in input mode by default, and ofstream opens files in output mode by default. the C ++ I/O system manages two pointers associated with one file. One is the read pointer, which indicates the position of the input operation in the file; the other is the write pointer, which indicates the position of the next write operation on the file. The corresponding pointer automatically changes each time the input or output is executed.
File pointer positioning: Use the member functions seekg () and seekp (), seekg () to set the read position, and seekp to set the write position.
Their most common forms are as follows:
Istream & seekg (off_type offset, seekdir origin );
Ostream & seekp (off_type offset, seekdir origin );
Offset offset; seekdir can be IOs: Beg (starting with the file), IOS: cur (current position of the file), IOS: end (end of the file ); these two functions are generally used in binary files, because text files may be different from the expected values due to the system's interpretation of characters.
Second:
Common file functions (none of them are output): fopen () fwrite () fread () fflush () fseek () ftell () fclose () Rewind (), etc.
File * fopen (
Const char * filename
,
Const char * Mode
);
Size_t fwrite (<br/> const void * buffer, <br/> size_t size, <br/> size_t count, <br/> file * stream <br/> ); <br/> size_t fread (<br/> void * buffer, <br/> size_t size, <br/> size_t count, <br/> file * stream <br/>); <br/> int fflush (<br/> file * stream <br/> ); // refresh the buffer <br/> int fseek (<br/> file * stream, <br/> long offset, <br/> int origin <br/> ); /* set the file pointer position. The origin can be seek_cur seek_end seek_set. <br/> (file start position) */<br/> long ftell (<br/> file * stream <br/> ); // return the current position of the file pointer <br/> int fclose (<br/> file * stream <br/>); <br/> void rewind (File * stream ); // equivalent to fseek (stream, 0l, seek_set );
Third:
Common Win32 functions (not written in the form parameter table) createfile () readfile () writefile () closehandle ()
Handle winapi createfile (_ in lpctstr lpfilename, _ in DWORD dwdesiredaccess, _ in DWORD dw1_mode, _ in using lpsecurityattributes, _ in DWORD dwcreationdisposition, _ in DWORD complete, _ in handle htemplatefile); bool winapi readfile (_ in handle hfile, _ out lpvoid lpbuffer, _ in DWORD nnumberofbytestoread, _ out lpdword lpnumberofbytesread, _ in lpoverlapped); bool winapi writefile (_ in handle hfile, _ in lpcvoid lpbuffer, _ in DWORD nnumberofbytestowrite, _ out lpdword success, _ in lpoverlapped); bool winapi closehandle (_ in handle hobject );
For more information, see Platform SDK.
Fourth (commonly used ):Cfile common member functions such as open () read () write () flush () getlength () Seek () seektobegin () seektoend ()
Constructor:
Cfile ();
Cfile (handle hfile );
Cfile (lpctstr lpszfilename, uint nopenflags );
Nopenflags can be one or more of the following combinations:
Cfile: modecreate directly constructs to create a new file. If this file exists, all content in this file will be deleted.
Cfile: modenotruncate
Joint creation attribute. If this file has been created, the original file content will not be deleted.
Cfile: moderead is read-only.
Cfile: Open modewrite to write-only attributes.
Cfile: Open modereadwrite as the read/write attribute.
Cfile: modenoinherit prevents this file from being generated by sub-processes.
Cfile: sharedenynone opens this file in addition to the read/write processing of this file.
Cfile: sharedenyread is opened exclusively and other read operations are rejected.
Cfile: sharedenywrite is enabled exclusively and Other write operations are rejected.
Cfile: volume Exclusive open the file in an exclusive mode and deny other read/write operations to access the file.
Cfile: The compat attribute is not available in 32 bit MFC.
Cfile: typetext sets text mode to specifically handle carriage return line breaks (used in Derived classes only ).
Cfile: typebinary sets binary mode (used in Derived classes only ).
Cfile: osnobuffer cfile: oswritethrough cfile: osrandomaccess cfile: ossequentialscan (I have also used these)
Virtual bool open (<br/> lpctstr lpszfilename, <br/> uint nopenflags, <br/> cfileexception * perror = NULL <br/> ); <br/> virtual uint read (<br/> void * lpbuf, <br/> uint ncount <br/> ); <br/> virtual void write (<br/> const void * lpbuf, <br/> uint ncount <br/>); <br/> virtual ulonglong getlength () const; // return the length of the file <br/> virtual ulonglong seek (<br/> Longlong Loff, // number of bytes to move the pointer <br/> uint nfrom // cfile: Begin cfile: Current cfile: End <br/> ); <br/> void seektobegin (); <br/> ulonglong seektoend (); // return the length of the file in bytes