[SOURCE DOWNLOAD]
Indispensable Windows Native-C + +: Files
Webabcd
Introduced
Essential C + + of Windows Native
Example
CppIO2.h
#pragma<string>usingnamespace std; namespace nativedll{ class CppIO2 { public: String Demo (string rootpath);} ;}
CppIO2.cpp
/** File*/#include"pch.h"#include"CppIO2.h"#include<fstream>//Ifstream (inherited from IStream), Ofstream (inherited from Ostream)using namespaceNativedll;voidcppio2_demo1 ();voidCppio2_demo2 ();voidCppio2_demo3 ();voidCppio2_demo4 ();string_rootpath;stringCppIO2::D Emo (stringRootPath) {_rootpath=RootPath; //writing to a text fileCppio2_demo1 (); //reading text FilesCppio2_demo2 (); //Writing binary FilesCppio2_demo3 (); //To read a binary fileCppio2_demo4 (); return "Save Path to demo file:"+_rootpath;}//writing to a text filevoidCppio2_demo1 () {stringFileName = _rootpath +"\\cpp_file_demo1.txt"; //Open File//ofstream outfile; //Outfile.open (FileName, ios::out); //Open file can also be written like thisOfstream outfile (FileName, iOS:: out);//The 2nd parameter is the input and output mode of the file (multiple "|" separated), which is described in the following comments//If you want to write and want to read, use FStream . if(!outfile)//if the file fails to open, the return value is 0 { //Err } for(inti =0; I <3; i++) { //writing Text dataOutFile <<"ABC"<<"\ n"; } //formatted text data for writingoutfile << Hex << -<< Std::endl;//for formatting see: CppIO1.cpp//Close FileOutfile.close (); }//reading text FilesvoidCppio2_demo2 () {stringFileName = _rootpath +"\\cpp_file_demo1.txt"; Charbuf[1024x768]; stringresult; Ifstream infile (FileName, iOS::inch);//The 2nd parameter is the input and output mode of the file (multiple "|" separated), which is described in the following comments//If you want to write and want to read, use FStream .//The file opened successfully, stating that the file existed before if(Infile.is_open ()) {//returns the next character, but the internal position pointer of the file does not change Charc = Infile.peek ();//If the returned character is a file terminator, its value is EOF//the file is normal and the internal location pointer is not at the end while(Infile.good () &&!infile.eof ())//Eof-end of Line{memset (buf,0,1024x768);//Empty bufInfile.getline (BUF,1204);//reads a row of data from the current file's internal position pointer to buf, and then the file internal position pointer points to the next lineresult+=buf; Result+="\ n"; } infile.close (); }}//Writing binary FilesvoidCppio2_demo3 () {structEmployee {intnum; Charname[ +]; } w[3] = { { -,"Wanglei"}, { $,"WEBABCD"}, { -,"Diandian" } }; stringFileName = _rootpath +"\\cpp_file_demo2.b"; Ofstream outfile (FileName, ios::binary); //The 2nd parameter is the input and output mode of the file (multiple "|" separated), which is described in the following comments//If you want to write and want to read, use FStream . if(!outfile) { //Err } for(inti =0; I <3; i++) { //Write Data (to convert the address to a char* type pointer)Outfile.write ((Char*) &w[i],sizeof(W[i])); } outfile.close ();}//To read a binary filevoidCppio2_demo4 () {structEmployee {intnum; Charname[ +]; } r[2]; stringFileName = _rootpath +"\\cpp_file_demo2.b"; Ifstream infile (FileName, ios::binary); //The 2nd parameter is the input and output mode of the file (multiple "|" separated), which is described in the following comments//If you want to write and want to read, use FStream . if(!infile) { //Err } //moves the internal position pointer of the file. For an action function on the internal location pointer of a file, see the following commentINFILE.SEEKG (sizeof(employee), ios::cur); for(inti =0; I <2; i++) { //read data (to convert the address to a char* type pointer)Infile.read ((Char*) &r[i],sizeof(R[i])); } infile.close ();}/** file input/Output mode: * * ios::in-Open File as input * ios::out-open File as output (this is the default), if there is a file with this name, then all its original contents clear * Ios::app-Open the file as output, write the number According to add at the end of the file * ios::ate-Open an existing file, the file pointer to the end of the file * ios::trunc-Open a file, if the file already exists, then delete all of the data, such as the file does not exist, then create a new file. If you have specified the Ios::out mode, and you do not specify Ios::app, Ios::ate, Ios::in, the default is this way * ios::binary-Opens a file in binary mode, which is ASCII by default if this method is not specified * ios::in | Ios::out-Open file as input and output, file readable writable * ios::out | Ios::binary-Open an output file in binary mode * ios::in | Ios::binar-Open an input file in binary mode*//** Operation function of the internal position pointer of the file: * * Gcount ()-Returns the number of bytes read in the last input * TELLG ()-Returns the current position of the input file pointer * SEEKG (position in file)-moves the pointer in the input file to the specified location * SEEKG (bit Shift, reference position)-moves several bytes based on the reference position * TELLP ()-Returns the current position of the output file pointer * SEEKP (position in file)-moves the pointer in the output file to the specified position * SEEKP (displacement, reference position)-moves several bytes based on the reference position * * * * * Refer to Location Description: * Ios::beg-the beginning of the file (beg is the abbreviation for begin), which is the default value * Ios::cur-the current position of the pointer (cur is the abbreviation of present) * Ios::end-end of file*/
Ok
[SOURCE DOWNLOAD]
Indispensable Windows Native-C + +: Files