C + + file stream operations must be known

Source: Internet
Author: User

One, file stream operation

1. Determine the mode in which the file opens. The main modes to choose from are:

Ios::in open a file for input (read)
Ios::out Open file for output (write)
Ios::ate initial Position: End of File
Ios::app all outputs appended to the end of the file
Ios::trunc if the file already exists, delete the file first
Ios::binary binary mode

2. By default, the file is written as text, and the data in the original file is deleted, i.e. Ios::trunc

3. Determine if the file is open properly. The good file operation habit is that each time a file is opened, before the file is written to determine whether the file is open normally, the function used is is_open ().

4. Write the file. There are three main functions,<< stream operator, write a character put (), write a piece of data writing;

std::ostream::operator<<
Std::ostream::p UT
Std::ostream::write

5. File read. The main stream operator >>, read a character get, read a line of getline, read a file

Std::istream::operator>>
istream& operator>> (int& val);
Std::istream::getline
istream& Getline (char* s, streamsize N);
istream& Getline (char* s, streamsize N, Char Delim);
Std::getline (String)
istream& Getline (istream& is, string& str, char delim);
istream& Getline (istream& is, string& str);
Std::istream::read
istream& Read (char* s, streamsize N);
Std::istream::get
istream& get (char& c);

6. End of file judgment infile.eof ()

7. File Close infile.close ()

8. File location seekp (), SEEKG ()

9. File modification, see example

1.1 File Write
#include <string>#include<iostream>#include<fstream>using namespacestd;intMain () {/** Ios::app: Add, Ios::trunc: If the file exists, first delete the file (default) * bios::binary binary Read-write, default is text mode*/ofstream outfile ("testfile.txt"Ios:: out|Ios::trunc); if(!Outfile.is_open ()) {Cerr<<"file cannot open!"<<Endl; return-1; } outfile<<"This is a test.\n"; OutFile<< -<<Endl; Outfile.put ('C');//write a char.Outfile.put ('\ n'); Charbuffer[1024x768] ="ABC"; Outfile.write (Buffer,sizeof(buffer));//write a block.Outfile.close (); return 0;}
1.2 File read
#include<string>#include<iostream>#include<fstream>using namespacestd;intMain () {Ifstream infile ("testfile.txt"); if(!Infile.is_open ()) {Cerr<<"file cannot open!"<<Endl; return-1; }    //read a character    Charch; InFile.Get(CH); //read a string    stringWord; InFile>>Word; //read a line commonly usedINFILE.SEEKG (0); stringLine ;    Getline (infile, line); Charbuffer[1024x768]; INFILE.SEEKG (0);//Navigate to File headerInfile.getline (Buffer,sizeof(buffer)); //Read File BlockINFILE.SEEKG (0); Infile.read (Buffer,sizeof(buffer)); //determine end of fileINFILE.SEEKG (0);  while(!infile.eof ()) {getline (infile,line);//infile >> Word;//Infile.read (buffer,sizeof (buffer));} infile.close (); return 0;}
1.3 File Content Modification
#include <fstream>usingnamespace  std; int Main () {    fstream inoutfile ("testfile.txt", iOS:: in | iOS::out);    INOUTFILE.SEEKG (+);     " It's easy to modify the file! \ n";    Inoutfile.close ();     return 0 ;}

C + + file stream operations must be known

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.