Windows Programming _ sun Xin C ++ lesson12 File Operations

Source: Internet
Author: User
Tags rewind

Windows Programming _ sun Xin C ++ lesson12 File Operations

Highlights of this section:
1. pointer to constant and pointer constant
2. Differences between binary files and text files
3. C language file operations
4. c ++ File Operations
5. Win32 API File Operations
6. File Operations of the MFC cfile class
7. File Save and open dialog box operations
8. ini file operations
9. Registry File Operations
//************************************** **************************************** *****************
1. pointer to constant and pointer constant
Pointer to a constant. The pointer value can be modified,
You cannot use this pointer to change the content it points to, but you can use the array itself to change its content. For example:
//************************************** **********
Char ch [5] = "Lisi ";
Const char * pstr = CH;
* Pstr = 'W'; // Error
Pstr = "wangwu" // yes. The pointer value can be changed.
//************************************** **********
Pointer constant. The pointer value cannot be modified, but its content can be modified. For example:
//************************************** **********
Char ch [5] = "Lisi ";
Char * const pstr = CH; // The initial value must be assigned when the pointer constant is defined.
Pstr = "zhangsan"; // error. The pointer value cannot be modified.
* Pstr = 'W'; // the content to be directed to can be modified.
//************************************** **********
2. Differences between binary files and text files
When you open a text file, you can see the files translated in ASCII code format. The binary file is saved in binary format.
File Read operations and write operations should follow the principle of consistent open mode (Binary write binary read, text write text form to open), otherwise problems may occur.
Understanding of text files and binary files is as follows:

 

The text and binary methods are described as follows:

3. C language file operations
(1) File Operations
Open File * fopen (const char * filename, const char * mode );
Write size_t fwrite (const void * buffer, size_t size, size_t count, file * stream );
Read size_t fread (void * buffer, size_t size, size_t count, file * stream );
Disable int fclose (File * stream );
File pointer to get long ftell (File * stream );
The file read/write pointer moves int fseek (File * stream, long offset, int origin );
The file pointer is relocated to the file header void rewind (File * stream );
In file read/write mode, the default text mode includes binary mode B, for example, Rb.
(2) Timely file writing
The buffer mechanism is introduced for reading and writing C language files. The Int fflush (File * stream) function must be used for timely file writing. You can also close a file, but you need to open and close the file frequently.
(3) file Terminator '\ 0' (otherwise, garbled display may occur)
//************************************** ******************
// When method 1 is written, one more byte is written, and the multiple written bytes are automatically filled with zero
Fwrite ("data to wirte", sizeof (char), strlen ("data to wirte") + 1, streamout );
//************************************** ******************
// Method 2 accept the buffer using the memset Function
Char chread [100];
Memset (chread, 0, sizeof (chread); // Add '\ 0' operation method 2
//************************************** ******************
// Method 3 obtain the file length and dynamically allocate memory
Char * pbuf;
Fseek (streamin, 0, seek_end );
Int Len = ftell (streamin );
Pbuf = new char [Len + 1];
The C language file operation experiment code is as follows:
//************************************** ******************
// Write operation
Void cfileview: onwritefile ()
{
// Todo: add your command handler code here
File * streamout = fopen ("1.txt"," W ");
If (streamout! = NULL)
{
// Add '\ 0' to write one byte at the end of the file.
Fwrite ("data to wirte", sizeof (char), strlen ("data to wirte") + 1, streamout );
Fclose (streamout); // close the file and re-open it next time.
}
}
// Read operation
Void cfileview: onreadfile ()
{
// Todo: add your command handler code here
/* Add '\ 0' operation method 2
File * streamin = fopen ("1.txt"," R ");
Char chread [100];
Memset (chread, 0, sizeof (chread ));//
If (streamin! = NULL)
{
Fread (chread, sizeof (char), 100, streamin); // write the file after the buffer mechanism program is closed
MessageBox (chread );
}
/* Add '\ 0' operation method 3
File * streamin = fopen ("1.txt"," R ");
Char * pbuf;
Fseek (streamin, 0, seek_end );
Int Len = ftell (streamin );
Pbuf = new char [Len + 1];
// Fseek (streamin, 0, seek_set); // pay attention to moving the file pointer to the file header again
Rewind (streamin); // place the beginning
Fread (pbuf, sizeof (char), Len, streamin );
Pbuf [Len] = '\ 0 ';
MessageBox (pbuf );
}
//************************************** ******************
4. c ++ File Operations
C ++ files are rarely used for operations.
The C ++ file operation code is as follows: contains the header file # include "fstream. H"
//************************************** ******************
// Write operation
Void cfileview: onwritefile ()
{
Ofstream ofs ("4.txt ");
OFS. Write ("file experiment", strlen ("file experiment "));
OFS. Close ();

}
// Read operation
Void cfileview: onreadfile ()
{
Ifstream ifs ("4.txt ");
Char szread [100];
Memset (szread, 0,100 );
IFS. Read (szread, 100 );
MessageBox (szread );
}
//************************************** ******************
5. Win32 API File Operations
Perform operations using Win32 functions provided by the SDK platform,
The file operation code is as follows:
//************************************** **************************************** *****************
// Write a file
Void cfileview: onwritefile ()
{
Handle hfile = createfile ("5.txt", generic_write, 0, null, create_new,

File_attribute_normal, null );
DWORD dwwrite;
If (hfile)
{
Writefile (hfile, "file experiment", strlen ("file experiment"), & dwwrite, null );
Closehandle (hfile );
}
}
// Read the file
Void cfileview: onreadfile ()
{
Handle hfile = createfile ("5.txt", generic_read, 0, null, open_existing,

File_attribute_normal, null );
DWORD dwread;
Char szread [100];
Memset (szread, 0,100 );
If (hfile)
{
Readfile (hfile, szread, 100, & dwread, null );
Closehandle (hfile );
MessageBox (szread );
}
}
//************************************** **************************************** *****************
6. File Operations of the MFC cfile class
//************************************** **************************************** *****************
The experiment operation code is as follows:
// Write a file
Void cfileview: onwritefile ()
{
Cfile file ("6.txt", cfile: modecreate | cfile: modewrite );
File. Write ("file experiment", strlen ("file experiment "));
File. Close ();
}
// Read the file
Void cfileview: onreadfile ()
{
Cfile file ("6.txt", cfile: moderead );
Char * pbuf;
DWORD Len = file. getlength ();
Pbuf = new char [Len + 1];
Pbuf [Len] = '\ 0 ';
File. Read (pbuf, Len );
File. Close ();
MessageBox (pbuf );
}
//************************************** **************************************** *****************
7. File Save and open dialog box operations
The cfiledialog class generated by the system is used for operations. Some parameters in the Setting dialog box are mainly implemented through the m_ofn member variable of the class. This is an openfilename struct, you can set some features of the dialog box by setting the values.
The experiment operation code is as follows:
//************************************** *************************************
// Use the Save dialog box to get the file name and write the file
Void cfileview: onwritefile ()
{
Cfiledialog filedlg (false );
Filedlg. m_ofn.lpstrtitle = "My save dialog box"; // set the title
Filedlg. m_ofn.lpstrfilter = "text files (*. txt) \ 0 *. txt \ 0all files (*. *) \ 0 *.*";
Filedlg. m_ofn.lpstrdefext = "TXT"; // default suffix
If (idok = filedlg. domodal ())
{
Cfile file (filedlg. getpathname (), cfile: modecreate | cfile: modewrite); // obtain the complete path name
File. Write ("file experiment", strlen ("file experiment "));
File. Close ();
}
}
// Open the file in the open dialog box
Void cfileview: onreadfile ()
{
Cfiledialog filedlg (true );
Filedlg. m_ofn.lpstrtitle = "my open dialog box ";
Filedlg. m_ofn.lpstrfilter = "text files (*. txt) \ 0 *. txt \ 0all files (*. *) \ 0 *. *"; // file type filtering settings
Filedlg. m_ofn.lpstrdefext = "TXT"; // default suffix
If (idok = filedlg. domodal ())
{
Cfile file (filedlg. getpathname (), cfile: moderead );
Char * pbuf;
DWORD Len = file. getlength ();
Pbuf = new char [Len + 1];
Pbuf [Len] = '\ 0 ';
File. Read (pbuf, Len );
File. Close ();
MessageBox (pbuf );
}
}
//************************************** *************************************
8. ini file operations
(1) Use Win32 global functions
The experiment code is as follows:
//************************************** *************************************
: Writeprofilestring ("profiles", "location", "China ");
Cstring MSG;
: Getprofilestring ("profiles", "location", "not found", MSG. getbuffer (100), 100 );
Afxmessagebox (MSG );
//************************************** *************************************
(2) Use functions of the cwinapp class
//************************************** *************************************
Writeprofilestring ("profiles", "location", "China ");
// Cstring MSG = getprofilestring ("profiles", "location", "not found ");
// Afxmessagebox (MSG );
//************************************** *************************************
9. Registry File Operations
Registry functions must be used for registry operations. Note that there are many parameters for registry functions.
The experiment code is as follows:
//************************************** **************************************** *****************
// Write to the Registry
Void cfileview: onwritereg ()
{
// Todo: add your command handler code here
Hkey = NULL;
DWORD dwage = 23;
Regcreatekey (HKEY_CURRENT_USER, "Software \ mfcfileex \ profiles", & hkey); // create a key
Regsetvalue (hkey, null, REG_SZ, "wangdingqiao", strlen ("wangdingqiao"); // set the default key value
Regsetvalueex (hkey, "Age", 0, REG_DWORD, (const byte *) & dwage, sizeof (DWORD); // set the key value of the [age] Key
Regclosekey (hkey); // close the key
}
// Read the Registry
Void cfileview: onreadreg ()
{
// Todo: add your command handler code here
Long Len;
DWORD dwage, dwtype, dwvalue;
Regqueryvalue (HKEY_CURRENT_USER, "Software \ mfcfileex \ profiles", null, & Len); // get its length
Char * pbuf = new char [Len + 1];
Regqueryvalue (HKEY_CURRENT_USER, "Software \ mfcfileex \ profiles", pbuf, & Len );
MessageBox (pbuf );
Hkey;
Regopenkey (HKEY_LOCAL_MACHINE, "Software \ mfcfileex \ profiles", & hkey); // open the registry key
Regqueryvalueex (hkey, "Age", null, & dwtype, (lpbyte) & dwage, & dwvalue); // query its value
Regclosekey (hkey );
Cstring MSG;
MSG. Format ("age = % d", dwage );
MessageBox (MSG );
}
//************************************** **************************************** *****************
The registry operation result is as follows:
//************************************** **************************************** *****************
Summary:
1. Understand the differences between pointers to constants and pointer constants in c ++
2. Identify the differences between binary files and text files
3. Fully understand file operations, be familiar with C, C ++, and Win32 file operation APIs, and Master cfile file operations in MFC.
4. Familiar with the operations of INI files and registry files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.