Methods for Operating Files in Windows Programming

Source: Internet
Author: User

Methods for Operating Files in Windows Programming

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
  1. Write File:
  2. File * pfile = fopen ("c.txt", "W"); // open the c.txt file in the written format.
  3. Fwrite ("Welcome to vcfans! ", 1, strlen (" Welcome to vcfans! "), Pfile); // write data to a file.
  4. Fflush (pfile); // refresh the buffer. Write buffer data to a file
  5. Fclose (pfile); // close the file
  6. Read files:
  7. File * pfile = fopen ("c.txt", "R"); // open the c.txt file as a reader.
  8. Char filecontent [100];
  9. Memset (filecontent, 0,100); // initialize filecontent
  10. Fread (filecontent, 1,100, pfile); // read the content in the just c.txt file to filecontent
  11. MessageBox (filecontent); // output result
  12. Fclose (pfile); // close the file

2. File Operations in C ++. Header file fstream. h to be included

C ++ code
  1. Write File:
  2. Ofstream ofs ("c00000000.txt"); // creates an ofstream object.
  3. OFS. Write ("Welcome to vcfans! ", Strlen (" Welcome to vcfans! "); // Write data to a file
  4. OFS. Close (); // close the ofstream object.
  5. Read files:
  6. Ifstream ifs ("c00000000.txt ");
  7. Char filecontent [100];
  8. Memset (filecontent, 0,100); // initialize filecontent
  9. IFS. Read (filecontent, 100); // read data
  10. IFS. Close (); // close the ifstream object
  11. MessageBox (filecontent); // output result

3. Win32 API function file operations. The header file WINBASE. h to be included. Class Library: kernel32.lib
  

C ++ code
  1. Write File:
  2. Handle hfile; // defines a handle.
  3. Hfile = createfile ("api.txt ",
  4. Generic_write,
  5. File_pai_write,
  6. Null,
  7. Create_new,
  8. File_attribute_normal,
  9. Null); // use the creatfile API function to open a file
  10. DWORD written;
  11. Writefile (hfile, "Welcome to vcfans! ", Strlen (" Welcome to vcfans! "), & Written, null); // write a file
  12. Closehandle (hfile); // close the handle
  13. Read files:
  14. Handle hfile; // defines a handle.
  15. Hfile = createfile ("api.txt ",
  16. Generic_read,
  17. File_pai_read,
  18. Null,
  19. Open_existing,
  20. File_attribute_normal,
  21. Null); // use the creatfile API function to open a file
  22. DWORD dwdatalen;
  23. Char filecontent [100];
  24. Readfile (hfile, filecontent, 100, & dwdatalen, null); // read data
  25. Filecontent [dwdatalen] = 0; // set the array to zero without the end.
  26. Closehandle (hfile); // close the handle
  27. MessageBox (filecontent); // output result

4. MFC cfile file operations. Header file afx. h to be included

C ++ code
  1. Write File:
  2. Cfile file ("cfile.txt", cfile: modecreate | cfile: modewrite); // construct a cfile object
  3. File. Write ("Welcome to vcfans! ", Strlen (" Welcome to vcfans! "); // Write data to the file
  4. File. Close (); // close the cfile object.
  5. Read files:
  6. Cfile file ("cfile.txt", cfile: moderead); // construct a cfile object
  7. Char filecontent [100];
  8. Memset (filecontent, 0,100); // initialize filecontent
  9. File. Read (filecontent, 100); // read data
  10. File. Close (); // close the file object
  11. MessageBox (filecontent); // output data

5. File Operations in the MFC cfiledialog class. Header file afxdlgs. h to be included

C ++ code
  1. Write File:
  2. Cfiledialog filedlg (false, "TXT", "cfiledialog.txt"); // create a cfiledialog object
  3. If (idok = filedlg. domodal ())
  4. {
  5. Cfile file (filedlg. getfilename (), cfile: modecreate | cfile: modewrite); // construct a cfile object
  6. File. Write ("Welcome to vcfans! ", Strlen (" Welcome to vcfans! "); // Write data to the file
  7. File. Close ();
  8. };
  9. Read files:
  10. Cfiledialog filedlg (true, "TXT", "cfiledialog.txt"); // create a cfiledialog object
  11. If (idok = filedlg. domodal ())
  12. {
  13. Cfile file (filedlg. getfilename (), cfile: moderead); // construct a cfile object
  14. Char filecontent [100];
  15. Memset (filecontent, 0,100); // initialize filecontent
  16. File. Read (filecontent, 100); // read data
  17. File. Close (); // close the file object
  18. MessageBox (filecontent );
  19. };

6. perform registry file operations.
  

C ++ code
  1. Write to registry:
  2. Hkey;
  3. DWORD dwsex = 1;
  4. Regcreatekey (HKEY_LOCAL_MACHINE, "software // vcfans // Reg", & hkey); // open the registry key
  5. Regsetvalueex (hkey, "sex", 0, REG_DWORD, (const byte *) & dwsex, 4); // write the registry data
  6. Regclosekey (hkey); // close the registry key
  7. Read the registry:
  8. Hkey;
  9. Regopenkey (HKEY_LOCAL_MACHINE, "software // vcfans // Reg", & hkey); // open the registry key
  10. DWORD dwtype;
  11. DWORD dwvalue;
  12. DWORD dwsex;
  13. Regqueryvalueex (hkey, "sex", 0, & dwtype, (lpbyte) & dwsex, & dwvalue); // Query registry data
  14. Regclosekey (hkey); // close the registry key
  15. Cstring STR;
  16. Str. Format ("Sex = % d", dwsex );
  17. 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.