C, C + +, MFC, Win32 read txt text information code __c++

Source: Internet
Author: User
Tags win32
File operation in 1.C language.
File operations in the 2.c++ language.
3.win32 API function file operation.
4.MFC CFile class file operation.
5.MFC CFileDialog The file operation of the class.
6. Registry file operation. Let me elaborate on the various file operation methods:


1. C-language file operation. required header files to include STDIO.H
Write to File:
File *pfile=fopen ("C.txt", "w");//open C.txt files in write mode.
Fwrite ("Welcome to Vcfans!", 1,strlen ("Welcome to Vcfans!"), pfile);//write data to file.
Fflush (pfile);//flush buffer. Write buffer data to a file
Fclose (pfile);//Close file
Read file:
File *pfile=fopen ("C.txt", "R");//open C.txt files in read mode.
Char filecontent[100];
memset (filecontent,0,100);//Initialize Filecontent
Fread (Filecontent,1,100,pfile)//Read the contents of the C.txt file to Filecontent
MessageBox (filecontent);//output result
Fclose (pfile);//Close file





File operations in the 2.c++ language. The header file to include Fstream.h
Write to File:
Ofstream ofs ("C++.txt");//establish ofstream to image.
Ofs.write ("Welcome to Vcfans!", strlen ("Welcome to Vcfans!")); /write data to file
Ofs.close ();//Close Ofstream object.
Read file:
Ifstream ifs ("C++.txt");
Char filecontent[100];
memset (filecontent,0,100);//Initialize Filecontent
Ifs.read (filecontent,100);//Read data
Ifs.close ()//close ifstream to Image
MessageBox (filecontent);//output result






3.win32 API function file operation. Need to include header file winbase.h, need class library: Kernel32.lib
Write to File:
HANDLE hfile;//defines a handle.
Hfile=createfile ("API.txt",
Generic_write,
File_share_write,
Null
Create_new,
File_attribute_normal,
NULL);//Use Creatfile this API function to open a file
DWORD written;
WriteFile (hfile, "Welcome to Vcfans!", strlen ("Welcome to Vcfans!"), &written,null);/write file
CloseHandle (hfile);//close handle
Read file:
HANDLE hfile;//defines a handle.
Hfile=createfile ("API.txt",
Generic_read,
File_share_read,
Null
Open_existing,
File_attribute_normal,
NULL);//Use Creatfile this API function to open a file
DWORD Dwdatalen;
Char filecontent[100];
ReadFile (hfile,filecontent,100,&dwdatalen,null);//Read data
filecontent[dwdatalen]=0;//sets the array to a trailing 0.
CloseHandle (hfile);//close handle
MessageBox (filecontent);//output result





4.MFC CFile class file operation. The header file to include Afx.h
Write to File:
CFile file ("CFile.txt", cfile::modecreate| Cfile::modewrite);//Construct CFile Object
File. Write ("Welcome to Vcfans!", and Strlen ("Welcome to Vcfans!")); /write data to file
File. Close ()//closes the CFile object.
Read file:
CFile file ("CFile.txt", cfile::moderead);//Construct CFile Object
Char filecontent[100];
memset (filecontent,0,100);//Initialize Filecontent
File. Read (filecontent,100);//reading data
File. Close ()//Closes file object
MessageBox (filecontent);//Output data






5.MFC CFileDialog The file operation of the class. The header file to include Afxdlgs.h
Write to File:
CFileDialog Filedlg (FALSE, "txt", "CFileDialog.txt");/Create CFileDialog Object
if (Idok==filedlg.domodal ())
{
CFile file (Filedlg.getfilename (), cfile::modecreate| Cfile::modewrite);//Construct CFile Object
File. Write ("Welcome to Vcfans!", and Strlen ("Welcome to Vcfans!")); /write data to file
File. Close ();
};
Read file:
CFileDialog Filedlg (TRUE, "txt", "CFileDialog.txt");//Create CFileDialog Object
if (Idok==filedlg.domodal ())
{
CFile file (Filedlg.getfilename (), cfile::moderead);//Construct CFile Object
Char filecontent[100];
memset (filecontent,0,100);//Initialize Filecontent
File. Read (filecontent,100);//reading data
File. Close ()//Closes file object
MessageBox (filecontent);
};





6. Registry file operation.
Write to the registry:
HKEY hkey;
DWORD dwsex=1;
RegCreateKey (HKEY_LOCAL_MACHINE, "Software\\vcfans\\reg", &hkey);//Open Registry key
RegSetValueEx (hkey, "sex", 0,reg_dword, (CONST byte*) &dwsex,4);/write Registry data
RegCloseKey (hkey);//Close registry key
Read the Registration form:
HKEY hkey;
RegOpenKey (HKEY_LOCAL_MACHINE, "Software\\vcfans\\reg", &hkey);//Open Registry key
DWORD dwtype;
DWORD dwvalue;
DWORD Dwsex;
RegQueryValueEx (hkey, "sex", 0,&dwtype, (LPBYTE) &dwsex,&dwvalue);//Query Registry data
RegCloseKey (hkey);//Close registry key
CString str;
Str. Format ("sex=%d", dwsex);
MessageBox (str)//The above code is compiled under Vc6.0,windows 2K server

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.