Sun Xin VC ++ video tutorial (12) File Operations

Source: Internet
Author: User

Char ch [5] = "list"
This assignment can only be performed when the array is defined. The constant string will automatically add/0, so the string length is five

Const char * pstr = CH; // only pointer values can be modified, but contents cannot be modified
A pointer to a constant indicates that the object to which the pointer is directed is a constant, the pointer value can be changed, and the pointing content cannot be changed.
Pstr = "flsdkjf"; // indicates to assign the address pointed to by the string to pstr;

Char * const pstr = CH; // pointer constant. The pointer value cannot be modified, but the content to which it points can be modified,
Fwrite (CH, 1, strlen (CH), pfile );
Fwrite (CH, 1, strlen (CH), pfile );
// The file pointer always points to the next address and two strings are displayed.

File write operations
File * pfile = fopen ("1.txt"," W "); // The content first in the open file will be destroyed.
Fwrite ("I love you", 1, strlen ("I Love You"), pfile );
Fclose (pfile );
Fclose (pfile); function is to close the file to end the buffer, because Windows read and write files are stored in the buffer, when the buffer is full before saving into the file, we can terminate the buffer in advance
But we don't want to close the file all the time. We can use another function fflush to refresh the buffer and write the buffer data into the file.

File Read operations
File * pfile = fopen ("1.txt"," R ");
Char ch [100] = "0"; // The array is assigned to all zeros.
// Char ch [100]; // if you do not set the array to zero, you can also write an empty character into the file.
// For example, fwrite ("I love you", 1, strlen ("I Love You") + 1, pfile );
// Memset (CH, 0,100); assigns an array to zero.
Fread (CH, 1,100, pfile );
Fflush (pfile );
MessageBox (CH );

Enter x = 10 on the screen, and 10 is a variable.
Two methods:
Cstring STR;
Str. Format ("x = % d", strlen (CH ));
MessageBox (STR );
And:
Char buffer [10];
Sprintf (buffer, "x = % d", strlen (CH ));
MessageBox (buffer );

To dynamically obtain the length of the original file, use the following code:
Char * pbuf;
Fseek (pfile, 0, seek_end); // move the file pointer to the end of the file
Int n = ftell (pfile); // get the file length
Rewind (pfile); // move the pointer back to the file header
Pbuf = new char [n + 1];
Pbuf [N] = 0; // Add/0 at the end of the file as the file Terminator
Fread (pbuf, 1, n, pfile );

ASCII code 10 is a linefeed 13 is a carriage return

Binary and text files
Files are stored in the external storage media in binary format in computer memory.
Files are generally divided into binary files and text files.
A binary file is a file containing data or program instructions written in ASCII and extended ASCII characters. It is generally executable programs, graphics, images, sound and other files.
Text Files (also known as ASCII files): each byte of a text file stores an ascii code that can be expressed as a single character. It is an information organization and storage file with the basic structure of "lines". It can be used by any text processing program to read simple text files.

Text and binary
When we write data into the King file by text method, once we encounter a line break character (ASCII is 10), it will be converted to a carriage return -- line feed (ASCII is 13, 10 ). When reading a file, once you encounter a combination of carriage return and line feed (that is, continuous ascii13 and 10), it will be converted to line feed characters (ASCII is 10 ).
When we write data to a file in binary mode, the data is output to the file in the memory storage format as is.

Output The number 98341 In the example in notepad.
File * pfile = fopen ("2.txt"," W ");
Char ch [5];
Ch [0] = 9 + 48;
Ch [1] = 8 + 48;
Ch [2] = 3 + 48;
Ch [3] = 4 + 48;
Ch [4] = 1 + 48;
Fwrite (CH, 1, 5, pfile );
Fclose (pfile );
Note that when the number is saved as ASCII code, the output will become unidentifiable characters, for example:
Int I = 98341; // to make it available, you can add ITOA (I, CH, 10 );
Fwrite (& I, 4, 1, pfile );
The above is the method for accessing files in Standard C.

C ++ file Writing Method
Header file # include "fstream. H"
Ofstream OS ("3.txt ");
OS. Write ("I love you! ", Strlen (" I love you! "));
OS. Close ();

Read files:
Ifstream ifs ("3.txt ");
Char ch [100] = "0 ";
IFS. Read (CH, 100 );
IFS. Close ();
MessageBox (CH );

Next we will use WIN32API function access:
Handle hfile;
Hfile = createfile ("6.txt", generic_write, 0, null, create_always, file_attribute_normal, null );
DWORD dwwrites;
Writefile (hfile, "I love you! (5) ", strlen (" I love you! (5) "), & dwwrites, null );
Closehandle (hfile );

MessageBox (CH );*/
Handle hfile;
Hfile = createfile ("6.txt", generic_read, 0, null, open_existing, file_attribute_normal, null );
Char ch [100] = "0"; // if you only want to write char ch [100], you can use CH [dwread] = 0 to set the Terminator.
DWORD dwread;
Readfile (hfile, CH, 100, & dwread, null );
Closehandle (hfile );
MessageBox (CH );

SDK Method
Cfile file ("7.txt", cfile: modecreate | cfile: modewrite );
File. Write ("I love you 1000", strlen ("I love you 1000 "));
File. Close ();

Cfile file ("7.txt", cfile: moderead );
Char ch [100] = "0 ";
DWORD I = file. getlength ();
File. Read (CH, I );
MessageBox (CH );
File. Close ();

Create a file dialog box. How to access the file:
Cfiledialog filedlg (false );
Filedlg. m_ofn.lpstrtitle = "my file storage dialog box ";
Filedlg. m_ofn.lpstrfilter = "text files (*. txt)/0 *. txt/0all files (*. *)/0 *. */0/0 ";
// Note the structure of lpstrfilter: Add/0 to the back of each paragraph, and add two/0 to the end. The brackets only show the fact that // is followed by/0, this filter is only used to filter visible files and cannot be saved in what you see.
Filedlg. m_ofn.lpstrdefext = "TXT ";
If (idok = filedlg. domodal ())
{
Cfile file (filedlg. getfilename (), cfile: modecreate | cfile: modewrite );
File. Write ("I love you 1000", strlen ("I love you 1000 "));
File. Close ();
}

Cfiledialog filedlg (true );
Filedlg. m_ofn.lpstrtitle = "my file opening dialog box ";
Filedlg. m_ofn.lpstrfilter = "text files (*. txt)/0 *. txt/0all files (*. *)/0 *. */0/0 ";
If (idok = filedlg. domodal ())
{
Cfile file (filedlg. getfilename (), cfile: moderead );
Char * pbuf;
DWORD I = file. getlength ();
Pbuf = new char [I + 1]; // It is worth learning to dynamically create a buffer.
Pbuf [I] = 0;
File. Read (pbuf, I );
MessageBox (pbuf );
File. Close ();
}
}

SDK function: writeprofilestring ("songpeng", "SP", "Song"); used to write data in C:/Windows/win. ini. On the one hand, to be compatible with sixteen-bit programs, on the other hand, to improve program running speed
Cstring STR;
: Getprofilestring ("songpeng", "SP", "peng", str. getbuffer (100), 100 );
Afxmessagebox (STR );
It is used to extract data from the corresponding data items in C:/Windows/win. ini.
Lptstr getbuffer (INT nminbuflength );
Returns
A pointer to the internal character buffer for the cstring object.
Returned lptstr is not const and thus allows direct modification
Cstring contents.
If you use the pointer returned by getbuffer
Change the string contents, you must call releasebuffer before using
Any other cstring member functions.

Setregistrykey (_ T ("Local Appwizard-generated Applications"); used in the Registry
HKEY_CURRENT_USER-> Add the primary key local Appwizard-generated applications under software
The subkeys and values are added by writeprofilestring ("songpeng", "SP", "Song ").

Cstring STR;
STR = getprofilestring ("songpeng", "SP ");
Afxmessagebox (STR );
Used to read the corresponding key value
Note that winapp does not have the MessageBox function. We need to use the global function afxmessagebox (STR );

The following describes how to modify the Registry in a program:
Long regsetvalueex (
Hkey, // handle to key to set value
Lpctstr lpvaluename, // name of the value to set
DWORD reserved, // Reserved
DWORD dwtype, // flag for Value Type
Const byte * lpdata, // address of value data
DWORD cbdata // size of value data
);
Write:
Hkey;
Regcreatekey (HKEY_LOCAL_MACHINE, "software // songpeng // SP", & hkey );
// Phkresult: pointer to a variable that represents es a handle to the opened key.
Regsetvalue (hkey, null, REG_SZ, "Song", strlen ("song "));
Regclosekey (hkey );
Note: regcreatekey can only create REG_SZ data. To create other data, you must call regcreatekeyex.
For example, regsetvalueex (hkey, "Age", 0, REG_DWORD, (const byte *) & dwage, 4 );
DWORD occupies four bytes, cbdata = 4

Read:
Long lvalue;
Regqueryvalue (HKEY_LOCAL_MACHINE, "software // songpeng // SP", null, & lvalue );
Char * Buf = new char [lvalue]; // note the buffer creation method.
Regqueryvalue (HKEY_LOCAL_MACHINE, "software // songpeng // SP", Buf, & lvalue );
MessageBox (BUF );

Long regqueryvalue (
Hkey, // handle to key to query
Lptstr lpsubkey,
// Name of subkey to query
Lptstr lpvalue, // buffer for returned string
Plong lpcbvalue // es size of returned string
);
If
Lpvalue is null, and lpcbvalue is non-null, the function returns
Error_success, and stores the size of the data, in bytes, in
Variable pointed to by lpcbvalue. This lets an application determine
The best way to allocate a buffer for the value's data.
Therefore, we need to call regqueryvalue twice. The length of the key value is queried for the first time, and the key value is obtained for the second time.

To obtain other data types
Call regqueryvalueex
 

Hkey;
Regopenkey (HKEY_LOCAL_MACHINE, "software // songpeng // SP", & hkey );
// Open the primary key
DWORD dwtype;
DWORD dwage;
DWORD dwvalue;
Regqueryvalueex (hkey, "Age", 0, & dwtype, (lpbyte) & dwage, & dwvalue );
Cstring STR;
Str. Format ("age = % d", dwage );
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.