C + + read-write file Flow instance program explanation

Source: Internet
Author: User

  This article mainly introduces C + + read-write file flow examples, we refer to the use of the bar

Mastering the methods of reading and writing text files understanding the methods of reading and writing binaries   C + + file streams:     Replication code code is as follows: FStream//File stream ifstream//input file stream ofstream//output file stream &N Bsp Creates a text file and writes the information//outputs the information to the file #include the same as the output on the screen <iomanip.h> #include <fstream.h> void Main () {Ofstream F1 ("D           : Me.txt ");                 Open the file for writing, if the file does not exist to create it if (!F1) return;     Open file failed to end run F1<<SETW << "name:" << "Cheap Oriental" <<endl;   Write file content using the Insert operator F1<<SETW << "Home Address:" << "Henan Zhengzhou" <<endl;                   F1.close (); Close file}     Run open file D:me.txt, its contents such as: Name: cheap Oriental Home Address: Henan Zhengzhou   File operation: Open File File name Note the slash in the path name should be double write, such as: "D:myfilesreadme.txt" File Open options: ios::in= 0x01,//for read, file not present create (ifstream default Open method) Ios::out = 0x02,//write, file does not exist then create, if the file already exists then empty the original content (ofstream default open way) Ios::ate = 0x04,//When the file is open, the pointer is at the end of the file. Can change the position of the pointer, often and in and out joint use Ios::app = 0x08,//write, file does not exist to create, if the file already exists in the original file content after writing new content, the pointer position always in the last Ios::trunc = 0x10,// Truncate file length to 0 (default) ios::nocreate = 0x20 before read/write,///file does not exist, error occurs, often and in or app Ios::noreplace = 0x40,//file exists with error, common and out joint use IOs::binary= 0x80//binary format File File Protection options: Filebuf::openprot;  The default compatible sharing mode Filebuf::sh_none;  Exclusive, do not share filebuf::sh_read; Read Shared filebuf::sh_write; A method that writes a shared open file specifies the filename and open mode Ifstream f ("D:12.txt", ios::nocreate) when calling the constructor.  The default is to open the file in Ios::in, and the operation fails when the file does not exist Ofstream F ("D:12.txt"); The default is to open the file in Ios::out fstream f ("D:12.dat", ios::in|ios::out|ios::binary); Open a binary file read-write using the Open member function FStream f; F.open ("D:12.txt", ios::out); Use the Open function to check for successful opening of multiple files using the same object: if (f) {...}//To Ifstream, Ofstream object available, FStream object unavailable. if (F.good ()) {...} failed: if (!f) {...}//! Operator has overloaded if (F.fail ()) {...} read-write operations can only read and write text files using the <<,>> operator. The use of binary files may result in an error. Functions that use function members get, put, read, write, and so on often with read are Gcount (), which is used to obtain the number of bytes actually read. Read-write binaries note that you must specify Ios::binary in the open mode, otherwise read and write errors with ReadWrite for read and write operations, and can not use the INSERT, extraction operators to operate, otherwise there will be errors.  Use the EOF () function to detect whether a file is read or not, use Gcount () to get the number of bytes actually read close the file with the member function, such as: F.close (); The use of the destructor object at the end of the lifetime checks whether the file is closed and closes the file that is not closed. Random Read and write files can read and write at the specified location of the file by moving the file read-write pointer.  SEEKG (absolute position);//absolute movement,//input stream operation SEEKG (relative position, reference position); Relative Operation Tellg ();//Returns the current pointer position SEEKP (absolute position);/absolute Movement,//output flow operation SEEKP (relative position, reference position); The relative Operation TELLP ()//Returns the current pointer position reference position: ios::beg= 0///relative to the file header ios::cur= 1///to the current position ios::end= 2/////to read and write the text file in relation to the end of the file/for the correct readout of the number of written files According to the data, it is best to have separate     copy code code as follows: #include <fstream.h> void Main () {FStream f ("D:try.txt", ios::out); f<<1 234<< ' <<3.14<< ' A ' << ' How to Are you '; Write Data f.close (); F.open ("D:try.txt", ios::in); int i; Double D; char c; Char s[20]; f>>i>>d>>c; Read Data f.getline (s,20); cout<<i<<endl; Show each data cout<<d<<endl; cout<<c<<endl; cout<<s<<endl; F.close ();     Run results:   1234 3.14 A How are your press any key to continue   display the contents of a text file   read one character at a time using get ()----- ---------------------------Scheme i     copy code as follows: #include <fstream.h> void Main () {ifstream fin ("D: Introduction. txt", Ios::nocreate); if (!fin) {cout<< "File open error!n"; return;} char c;  while ((C=fin.get ())!=eof) cout<<c; Pay attention to the conclusion of the condition of Fin.close (); }  //Using GET (char *,int n,char delim= ' n ') read more than one character at a time----scheme two//clever use of characters in a text file without character "" read #include <fstream.h> void Main () { Ifstream Fin ("D: Profile. txt", ios::nocreate); if (!fin) {cout<< "File open error!n"; return;} Char c[80]; while (Fin.get (c,80, ')!=null) cout<<c; Pay attention to the conclusion of the condition of Fin.close (); ///using read (char *,int N) reading file---------------------------Scheme three #include <fstream.h> void Main () {ifstream fin ("D: Introduction. txt ", ios::nocreate); if (!fin) {cout<< "File open error!n"; return;} Char c[80]; while (!fin.eof ())//Determine whether the file is read over {fin.read (c,80); Cout.write (C,fin.gcount ()); } fin.close (); }       Copy file binary operation example     copy code code as follows: #include <fstream.h> void Main () {ifstream fin ("c:1.e Xe ", ios::nocreate|ios::binary); if (!fin) {cout<< "File open error!n"; return;} ofstream fout ("C:2.exe", ios::binary); Char c[1024]; while (!fin.eof ()) {fin.read (c,1024); Fout.write (C,fin.gcount ());} fin.close (); Fout.close (); cout<< "Copy over!n"; }

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.