Document and registry file operations: the difference between binary files and text files. Binary files move the data in the memory to the file to the file, and the text file is moved to the file. First make a menu of reading and writing, responding in CXXVIEW 1. C method: fWRITE: SIZE: ITEM SIZE IN BYTES Write how many bytes of byte count: maximum number of items to be written, a total of a total of a total of a total of several times.
File * p; p = fopen ("c: //1.txt", "w"); fwrite ("abc", 1, 4, p); fclose (p); due to the file after we open the file, the file is mapped In the cache in the memory, our operations made to the file are completed in memory. If we don't close the file, the modifications made to the file in memory will not reflect (save) to the hard disk, unless you Close the current application, this is the operation that is automatically executed. Fflush can empty the data in a stream (buffer) in the case where the file is not closed, where the emptying is to output the data of the buffer to the hard disk. This can reach the effect of the side-write output. File * pfile = fopen ("c: //1.txt", "w"); FWRITE ("Hunter Network", 1, Strlen ("Hunter Network"), Pfile; // Fclose (Pfile); Fflush (Pfile); FWRITE ("How Are You", Strlen ("Hunter Network"), Pfile; Fflush (Pfile); Fclose (Pfile); We found the next "H" is written The "heart" is behind, because it is a pointer to the file character position for the file, this pointer is different from the file pointer, which is the char * _PTR; member. When we finished the word "heart", this pointer is after the word "heart", so the next time "H" is written behind "Heart". If you want to output the second sentence in front of the "dimension", you want to move the file pointer, with fseekfile * pfile = fopen ("c: //1.txt", "w"); fwrite ("Hunter Network ", Strlen (" Hunter Network "), Pfile; // Fclose (Pfile); FFlush (Pfile); FSeek; FWRITE (" Beijing ", 1, Strlen (" Beijing " ), pflush; fseek; fseek; cstring str; str.format ("file size:% d", ftell (pfile); messagebox (str); fclose (pfile); Read file file * pfile = fopen ("c: //1.txt", "r"); char BUF [100]; FREAD (BUF, 1, 100, PFile); // Although the read data is exceeded in actual string The length, but is still looking for '/ 0' MessageBox (buf); fclose (pfile); there is a garbled because there is no time to find '/ 0' when the file is output, change the strull written by the file to the SIZEOF file When the write function needs to read and write, the '/ 0' belt, which is similar to the Printf and Strlen, which are indicated by '/ 0' as a function.
The solution of garbled can also be used with file * pfile = fopen ("c: //1.txt", "r"); char buf [100]; fseek (pfile, 0, seek_ek_ek_ek; long len = ftell (pfile); Rewind (Pfile); Fread (BUF, 1, LEN, PFILE); BUF [LEN] = 0; MessageBox (BUF); Fclose (pfile); third method: file * pfile = fopen ("c: // 1 .txt "," r "); Char BUF [100]; MEMSET (BUF, 0, 100); // can populate this memory block with any character. ZeromeMory (BUF, 100); // can only populate this memory block with '/ 0' characters. Fread (BUF, 1,100, Pfile); MessageBox (BUF); Fclose (Pfile); 2. C way: #include "fstream.h" Write: OFStream OFS ("C: //1.txt"); OFS.WRITE ("Hunter Network", Sizeof ("Hunter Network"); OFS.CLOSE (); / / It is best to turn off the file, it is not ridiculous, the destructor of this filebuf object is for you. Read: ifstream IFS ("C: //1.txt"); Char BUF [100]; IFS.Read (buf, 100); MessageBox (buf); when we write code to OFStream OFS ("C: / /1.txt "); char STR [3]; STR [0] = 'a'; STR [1] = 10; STR [2] = 'b'; OFS.WRITE (STR, SIZEOF (STR)); OFS.SEEKP (0); OFS.WRITE ("China", SIZEOF ("China")); found that the default is not compliance when writing and reading by the text at this time. This is because when you read and write in a text file, you will be converted. When you write, you will write 10 before adding 13 to the file, read the file to read 13 and 10, Change the two characters to a 10. Note Do not turn into a DOS format when you look at UltraEdit. If you read and write in binary (ios :: binary), there is no such problem. Do not do any conversion. C file operation Opening file is done in the constructor, and the shutdown file is done in the destructor.
3. MFC method: i. Write file: cfile f ("c: //1.txt", cfile :: modewrite | cfile :: modecreate); F.Write ("Hello", 5); a. Several logo Role: cfile :: MODECREATE: Do not specify a new file, open this file, cut it to 0; cfile :: modenotruncate: Do not crop the 0 when opening the file; b. Write data to the end of the file : Cfile f ("c: //1.txt", cfile :: modewrite | cfile :: modecreate | cfile :: modenotruncate; f.seektoend (); f.write ("Hello", 5); // file .Close (); if I don't close, the destructor will turn off for me. II. Reading Document: CFILE F ("C: //1.txt", CFile :: ModeRead; Char BUF [10]; MEMSET (BUF, 0, 10); F.Read (BUF, 5); MessageBox BUF); III. File dialog: Save dialog: cfiledialog fdlg (false); // fdlg.m_ofn.lpstitle = "hunting fox!"; fdlg.m_ofn.lpstrdeFext = "txt"; fdlg.m_ofn.lpstrfilter = "Text file (* .txt) / 0*.txt/0 All files (*. *) / 0 *. * / 0/0"; if (IDOK == fdlg.domodal ()) {// MessageBox (fdlg .GETFILENAME (); cfile file (fdlg.getFileName (), cfile :: modecreate | cfile :: modeWrite; File.Write ("Hunter Network", Sizeof ("Hunter Network"); file.close }
Open dialog:
CFiledialog FDLG (TRUE); // fdlg.m_ofn.lpstitle = "Hunter Manufacturing!"; Fdlg.m_ofn.lpstrfilter = "text file (* .txt) / 0*.txt/0 All files (*. *) / 0 *. * / 0/0 "; if (idok == fdlg.domodal ()) {cfile file (fdlg.getFileName (), cfile :: modeRead; char buf [100]; file.read (buf, 100 MessageBox (BUF);} 2. Differences for text files and binary files: File file is a special binary file, when it encounters the Enter key 10, it will automatically add a 13 in front of its front, while reading the file. When the combination of 13 10, it reduces it to 10. The binary file is to write the data into the file, and then read it again, and there is no such conversion operation of the text file. The following code demonstrates this difference: When writing a file: OFSTream F ("C: //1.txt"); char buf [3]; buf [0] = 'a'; buf [1] = '/ n'; buf [2] = 'b'; F.Write (buf, 3); when reading the file: ifstream f ("c: //1.txt"); f.setMode (filebuf :: binary); char buf [5]; MEMSET (BUF, 0, 5); F.Read (BUF, 5); CString Str; str.format ("% D,% D,% D,% D", BUF [ 0], BUF [1], BUF [2], BUF [3]); MessageBox (STR); does not specify in the format when writing files, files will be stored in text format, specifying binary formats when reading files. The read data is as follows: If you comment f.setMode (filebuf :: binary); statement, the file will be read according to the text file, as shown below: Second, the operation 1 of the registry 1. Read and write Win.ini files: Use the API's getProfileint and WriteProfileString to implement an example of a save window size. In the CMainFrame void CMainFrame :: OnDestroy () {CFrameWnd :: OnDestroy (); // TODO: Add your message handler code here CRect rect; GetWindowRect (& rect); CString str; str.Format ( "% d", rect. Width ()); WriteProfileString ("Window Size", "Width", Str); Str.Format ("% D", Rect.Height ()); WriteProfileString ("Window Size", "Height", Str);}
In the CMainFrame BOOL CMainFrame :: PreCreateWindow (CREATESTRUCT & cs) {if return FALSE; // TODO (CFrameWnd :: PreCreateWindow (cs)!): Modify the Window class or styles here by modifying // the CREATESTRUCT cs cs.cx = GetProfileInt ("Window Size", "Width", 100); CS.CY = GetProfileint ("Window Size", "Height", 100); Return True;} Demo uses the API's getProfileString. For its fourth parameter lpreturnedString needs Add a char * to return. Here you cannot add a CString object to return, this is a special place. Other functions can be replaced with a CString object when CHAR * is required. Here we add this char * with CString GetBuffer. A CSTRING OBJECT Consists of a variable-length sequence of character consists of a character string object consists of a variable length.
Pointer Returns a pointer to the internal character buffer for the CString object. The returned LPTSTR is not const and thus allows direct modification of CString contents. Returns a CString object internal character buffer (character array), the pointer is not returned to a The constant pointer, thus allowing the contents of the CSTRING object pointing directly to the pointer. This pointer and the address of the CSTRING internal character array are equal.
If you use the pointer returned before by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions. If you use the pointer returned by GetBuffer change the contents of the string, you use CString other member functions ReleaseBuffer must be called.
In cwinapp's InitInstance, CString Str; :: GetProfileString ("Window Size", "Height", "No Value", Str.GetBuffer (100), AFXMessageBox (STR); below Test CWinapp WriteProfileString, getProfileString. For WriteProfileString Some Description · In Windows NT, The Value Is Stored to a Registry Key. In Windows 3.x, The Value Is Stored in The Win.ini File. · In Windows 95, The Value Is Stored in a Cached Version Of win.ini in the cwinapp's InitInstance WriteProfileString ("Hunter", "VC ", "Rookie");
CString Str; Str = GetProfileString ("Hunter", "VC ", "Invalid Value"); AFXMessageBox (STR); So here is written to HKEY_CURRENT_USER / SOFTWARE / LOCAL AppWizard-generated application / myfile / rendezvous. Implement a simple counter to limit the number of software: setRegistryKey (_T ("myboleApp")); int x = getprofileint ("test", "times", 0); if (x> = 5) Return False; WriteProfileint ("TEST", "Times", X); CString Str; str.format ("You can use% D times", 5-x); AFXMessageBox (STR);
2. Read and write the WIN32 registry, do two menus to read and write the registry, write the handle to the operation key, which is the handle of the return operation key, with regcreateKey (this handle contains the primary key and the subkey, the first parameter can be An open handle or a predefined handle value, if it is the previously opened handle, then create a new handle under this open button according to the parameters of this open handle and the back subkey. ), Then read and write based on the handle. When using RegSetValue, the type of writes must be REG_SZ. This type can understand the string ended with '/ 0', if we want to write other data types, use RegSetValueex.The RegSetValue Function Sets the Data for The Default or Unnamed Value of a Specified Registry Key. The Data Must Be a Text String.RegSetValue function Sets the data for the default or no name specified registry key, this data must be a string. RegSetValue The last parameter does not include '/ 0' Using new functions Read and write any position in the registry: Write: HKEY HKEY; RegcreateKey (HKEY_LOCAL_MACHINE, "Software // Fox", & HKEY); // RegSetValue (HKEY, NULL, REG_SZ , "Weixin", 6); // RegSetValue (HKEY, "Course", REG_SZ, "Weixin", 6); DWORD I = 100; // The following parameters are the address, so this is to define this variable regsetValueex ( HKEY, "JSP", NULL, REG_DWORD, (const Byte *) & I, 4); RegcloseKey (HKEY); Read: in Bytes in bytes
Read a default key: char * buf; long Len; regQueryvalue (HKEY_LOCAL_MACHINE, "Software // 狐 网 //milyhu // ABC", NULL, & LEN); buf = new char [len]; regQueryvalue (hkey_local_machine, "Software // Hunter Network // MYLYHU // ABC", BUF, & LEN); MessageBox (BUF); Delete [] BUF; RegQueryValue Parameter Description: If LPVALUE IS NULL, AND LPCBVALUE IS NULL, THE FUNCTION RETURNS ERROR_SUCCESS , and stores the size of the data, in bytes, in the variable pointed to by lpcbValue. this lets an application determine the best way to allocate a buffer for the value's data. If lpValue is NULL, and lpcbValue is not NULL, the function returns Error_Success, and the size of the byte unit of the data is stored by the pointer of this variable representing the LPCBValue, which is the value of a name that is named in the data allocation space of the query value to the query value by the best way. HKEY hKey; RegCreateKey (HKEY_LOCAL_MACHINE, "software // Restoration", & hKey); DWORD dwType; DWORD data; DWORD len = 4; RegQueryValueEx (hKey, "JSP", NULL, & dwType, (BYTE *) & data, & len); CString Str; str.format; messagebox (str); :: regclosekey (HKEY); lock registry: hkey hkey; regreatekey (HKEY_CURRENT_USER, "Software // Microsoft // Windows // CurrentVersion /// Policies // System ", & HKey); DWORD X = 1; RegSetValueex (HKEY," DisableregistryTryTools ", 0, Reg_dWord, (const Byte *) & x, 4); RegcloseKey (HKEY);
Solutions registry HKEY hKey; RegCreateKey (HKEY_CURRENT_USER, "software // microsoft // windows // currentversion policies // system //", & hKey); DWORD x = 0; RegSetValueEx (hKey, "DisableRegistryTools", 0, REG_DWORD, ( Const Byte *) & X, 4); RegcloseKey (HKEY);