In Windows programming, file operations include the following common methods: 1. File Operations in C language. 2. File Operations in C ++. 3. Win32 API function file operations. 4. MFC cfile file operations. 5. File Operations in the MFC cfiledialog class. 6. perform registry file operations. The following describes the operation methods of various files in detail: 1. File Operations in C language. The header file to be included stdio. h C ++ code
- 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. File Operations in C ++. Header file fstream. h to be included
C ++ code
- 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 API function file operations. The header file WINBASE. h to be included. Class Library: kernel32.lib C ++ code
- 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 cfile file operations. Header file afx. h to be included
C ++ code
- 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. File Operations in the MFC cfiledialog class. Header file afxdlgs. h to be included C ++ code
- 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. perform registry file operations. C ++ code
- 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 );
|