The following describes how to operate files in the Standard C ++ and C run-time libraries. The disadvantages are obvious. We need to perform many operations and verification on our own.
In Windows, the system encapsulates these runtime libraries and then provides several available APIs to operate files.
Similarly, Windows sdks are mostly process-oriented packages. The following code is directly used:
Read files:
Char filename [50]; <br/> cout <"pleast input the file name you want to read:/N"; <br/> CIN> filename; <br/> wchar wszusername [256]; // Unicode user name <br/> // convert ANSI user name and domain to Unicode <br/> multibytetowidechar (cp_acp, 0, filename, <br/> strlen (filename) + 1, wszusername, <br/> sizeof (wszusername)/sizeof (wszusername [0]); <br/> handlehfile; <br/> dworddwfilesize; <br/> dworddwbytesread; <Br/> hfile = createfile (wszusername, // file to open <br/> generic_read, // open for reading <br/> file_assist_read, // share for reading <br/> null, // default security <br/> open_existing, // existing file only <br/> file_attribute_normal, // normal file <br/> null); // No ATTR. template <br/> If (hfile = invalid_handle_value) <br/>{ <br/> cout <"cocould not open file (error :)/N" <getlasterror (); <br/> Return; <br/>}< br/> dwfilesize = getfilesize (hfile, null); <br/> char * strinfo = new char [dwfilesize + sizeof (char)]; <br/> memset (strinfo, 0, (dwfilesize + sizeof (char) * sizeof (char); // clear <br/> If (! Readfile (hfile, strinfo, dwfilesize, & dwbytesread, null )) <br/>{ <br/> cout <"cocould not read from file (error :)/N" <getlasterror (); <br/> return; <br/>}< br/> int Len = strlen (strinfo); <br/> for (INT I = 0; I <Len; ++ I) <br/>{< br/> cout <strinfo [I]; <br/>}</P> <p> closehandle (hfile ); <br/> Delete [] strinfo;
Write File:
Cout <"pleast input the file name you want to write:/N"; <br/> char filename [50]; <br/> CIN> filename; <br/> wchar wszusername [256]; // Unicode user name <br/> // convert ANSI user name and domain to Unicode <br/> multibytetowidechar (cp_acp, 0, filename, <br/> strlen (filename) + 1, wszusername, <br/> sizeof (wszusername)/sizeof (wszusername [0]); <br/> DWORD dwresult; <br/> handle hfile; <br/> hfile = createfi Le (wszusername, // name of the write <br/> generic_write, // open for writing <br/> 0, // do not share <br/> null, // default security <br/> create_always, // overwrite existing <br/> file_attribute_normal, // normal file <br/> null); // No ATTR. template <br/> If (hfile = invalid_handle_value) <br/>{< br/> cout <"cocould not open file (error :)" <getlasterror (); <br/> return; <br/>}< br/> cout <"Please Indium Ut the data you want to write:/N "; <br/> char inputdata [100]; <br/> while (CIN> inputdata) <br/>{< br/> If (inputdata [0] = 'q') <br/>{< br/> closehandle (hfile ); <br/> return; <br/>}< br/> If (! Writefile (hfile, inputdata, strlen (inputdata), & dwresult, null )) <br/>{ <br/> cout <"cocould not write to file (error :)" <getlasterror (); <br/> return; <br/>}< br/> If (! Writefile (hfile, "/R/N", strlen ("/R/N"), & dwresult, null )) <br/>{ <br/> cout <"cocould not write to file (error :)" <getlasterror (); <br/> return; <br/>}</P> <p> closehandle (hfile); <br/>
The sample code is also simple, but you need to pay attention to their parameters. For the significance of the parameters, refer to the description of msdn, which has a very detailed description and examples.
Because my project attribute is unicode encoded, I used
Wchar wszusername [1, 256]; // Unicode <br/> multibytetowidechar (cp_acp, 0, filename, <br/> strlen (filename) + 1, wszusername, <br/> sizeof (wszusername)/sizeof (wszusername [0]);
You can also modify the encoding according to your own engineering environment. You can also use the ANSI or Unicode version of The Write function.
Same as above, as shown in figureCreatefilew
(UNICODE) andCreatefilea
(ANSI). For detailed descriptions of these functions, see the description on msdn.
Appendix, sample code of this series
The code is tested and passed in vs2008 + xpsp3.