Methods for file operations in Windows Programming

Source: Internet
Author: User

 

In Windows programming, file operations include the following common methods:
1. cLanguage file operations.
2. c ++
Language file operations.
3. Win32 API
Function file operations.
4. MFC cfile
Class file operations.
5. MFC cfiledialog
Class file operations.
6.
Registry file operations.

The following describes the operation methods of various files in detail:
1. cFile Operations in languages.Header file to be includedStdio. h
Write File:
File * pfile = fopen ("c.txt", "W"); // open the c.txt file in the written format.
Fwrite ("Welcome to vcfans! ", 1, strlen (" Welcome to vcfans! "), Pfile); // write data to a file.
Fflush (pfile); // refresh the buffer. Write buffer data to a file
Fclose (pfile); // close the file
Read files:
File * pfile = fopen ("c.txt", "R"); // open the c.txt file as a reader.
Char filecontent [100];
Memset (filecontent, 0,100); // initialize filecontent
Fread (filecontent, 1,100, pfile); // read the content in the just c.txt file to filecontent
MessageBox (filecontent); // output result
Fclose (pfile); // close the file

2. c ++Language file operations. Header file to be includedFstream. h
Write File:
Ofstream ofs ("c00000000.txt"); // creates an ofstream object.
OFS. Write ("Welcome to vcfans! ", Strlen (" Welcome to vcfans! "); // Write data to a file
OFS. Close (); // close the ofstream object.
Read files:
Ifstream ifs ("c00000000.txt ");
Char filecontent [100];
Memset (filecontent, 0,100); // initialize filecontent
IFS. Read (filecontent, 100); // read data
IFS. Close (); // close the ifstream object
MessageBox (filecontent); // output result

3. Win32 APIFunction file operations. Header file to be includedWINBASE. H,Class Library required:Kernel32.lib
Write File:
Handle hfile; // defines a handle.
Hfile = createfile ("api.txt ",
Generic_write,
File_pai_write,
Null,
Create_new,
File_attribute_normal,
Null); // use the creatfile API function to open a file
DWORD written;
Writefile (hfile, "Welcome to vcfans! ", Strlen (" Welcome to vcfans! "), & Written, null); // write a file
Closehandle (hfile); // close the handle
Read files:
Handle hfile; // defines a handle.
Hfile = createfile ("api.txt ",
Generic_read,
File_pai_read,
Null,
Open_existing,
File_attribute_normal,
Null); // use the creatfile API function to open a file
DWORD dwdatalen;
Char filecontent [100];
Readfile (hfile, filecontent, 100, & dwdatalen, null); // read data
Filecontent [dwdatalen] = 0; // set the array to zero without the end.
Closehandle (hfile); // close the handle
MessageBox (filecontent); // output result

4. MFC cfileClass file operations. Header file to be includedAfx. h
Write File:
Cfile file ("cfile.txt", cfile: modecreate | cfile: modewrite); // construct a cfile object
File. Write ("Welcome to vcfans! ", Strlen (" Welcome to vcfans! "); // Write data to the file
File. Close (); // close the cfile object.
Read files:
Cfile file ("cfile.txt", cfile: moderead); // construct a cfile object
Char filecontent [100];
Memset (filecontent, 0,100); // initialize filecontent
File. Read (filecontent, 100); // read data
File. Close (); // close the file object
MessageBox (filecontent); // output data

5. MFC cfiledialogClass file operations. Header file to be includedAfxdlgs. h
Write File:
Cfiledialog filedlg (false, "TXT", "cfiledialog.txt"); // create a cfiledialog object
If (idok = filedlg. domodal ())
{
Cfile file (filedlg. getfilename (), cfile: modecreate | cfile: modewrite); // construct a cfile object
File. Write ("Welcome to vcfans! ", Strlen (" Welcome to vcfans! "); // Write data to the file
File. Close ();
};
Read files:
Cfiledialog filedlg (true, "TXT", "cfiledialog.txt"); // create a cfiledialog object
If (idok = filedlg. domodal ())
{
Cfile file (filedlg. getfilename (), cfile: moderead); // construct a cfile object
Char filecontent [100];
Memset (filecontent, 0,100); // initialize filecontent
File. Read (filecontent, 100); // read data
File. Close (); // close the file object
MessageBox (filecontent );
};

 
6.Registry file operations. 
Write to registry:
Hkey;
DWORD dwsex = 1;
Regcreatekey (HKEY_LOCAL_MACHINE, "software // vcfans // Reg", & hkey); // open the registry key
Regsetvalueex (hkey, "sex", 0, REG_DWORD, (const byte *) & dwsex, 4); // write the registry data
Regclosekey (hkey); // close the registry key
Read the registry:
Hkey;
Regopenkey (HKEY_LOCAL_MACHINE, "software // vcfans // Reg", & hkey); // open the registry key
DWORD dwtype;
DWORD dwvalue;
DWORD dwsex;
Regqueryvalueex (hkey, "sex", 0, & dwtype, (lpbyte) & dwsex, & dwvalue); // Query registry data
Regclosekey (hkey); // close the registry key
Cstring STR;
Str. Format ("Sex = % d", dwsex );
MessageBox (STR );

 

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.