"C++primer" Review--with c++11 [3]

Source: Internet
Author: User
Tags truncated

1. Our programs often use many IO libraries for input and output such as IStream (input stream) types, which provide input operations.

Ostream (output stream) type, which provides an output operation.

CIN, a IStream object that reads data from standard input.

cout, a Ostream object that writes data to the standard output.

Cerr, a Ostream object that is typically used to output program error messages and write to standard errors.

The >>/<< operator, which is used to input and output data from a Istream/ostream object.

You cannot copy and assign a value to an Io object, because the IO object cannot be copied, so you cannot set the parameter or return type to the stream type. Functions that perform IO operations typically pass and return flows in a reference manner. Reading and writing an IO object changes its state, so the reference that is passed and returned cannot be const.

Flush the output buffer: Before the Endl completes the line break and flushes the buffer through the manipulator, two similar operations flush and ends,flush flush the buffer, but do not output any extra characters, ends output a null character to the buffer and flush the buffer.

The tie method can associate a stream to another stream, for example: Cin.tie (&cout); Ostream *old_tie = Cin.tie (nullptr);

Cin.tie (&cerr); Cin.tie (Old_tie);

2. File input and output, header file FStream defines three types to support files, Io:ifstream reads data from a given file, Ofstream writes data to a given file, and FStream can read and write a given file.

 1  fstream fstrm; //  2  fstream fstrm (s); //  Create a fstream and open a file named S, s can make a string type, or a pointer to a carton C-style string.  3  fstream fstrm (s, mode); //  Specify mode to open the file  4  Fstream.open (s); //  5  fstrm.close (); Close the file that is bound to FSTRM  6  Fstrm.is_open (); //  Returns a bool where the file associated with FSTRM is open successfully and has not been closed  
1 inch (ifile); 2  out ; 3  out " . Copy ");

When a FStream object is destroyed, close is automatically called

3. File mode, each stream has an associated file mode, where to use the file, the following table lists the file patterns and their meanings

inch                     Open out as read                    write open app                 each write operation before the army navigates to the end of the file ate                  open file immediately after the file is located to the end of trunc               truncate files binary             io in binary mode

Open file in Out mode will lose existing data, by default, when we open a ofstream, the contents of the file will be discarded, the organization of a ofstream to empty the given file content given method is the colleague designated app mode.

//in these statements, the file1 will be truncated.Ofstream out("file1");//suppress open file in output mode and truncate fileOfstream Out2 ("file1", Ofstream:: out);//suppressed truncated fileOfstream OUT3 ("file1", Ofstream:: out|ofstream::trunc);//to preserve the contents of the file, display the specified app modeOfstream app ("file2", Ofstream::app);//implied as output modeOfstream APP2 ("file2", Ofstream:: out| Ofstream::app);

Each call to open determines the file mode, and for a stream, each time you open the file, you can change its file mode.

 out // Open mode not specified out. Open ("scratchpad"); // mode implicitly set to output and truncate  out . Close (); out. Open ("precious"// mode for output and append

4.istringstream and Ostringstream bind a string object that can be used by the << and >> methods to enter the contents of a string object, which is like a piece of memory area bound to a string, When you use the >> method to write a C-style string or string, it also to a buffer, temporarily hold your input and output, and then call his Str method to return the whole piece of string. Two examples are as follows:

1 structPersonInfo2{stringname;3vector<string>phone;4 };5 stringLine , Word;6Vector<personinfo>people;7  while(Getline (CIN, line))8 {PersonInfo info;9 Istringstream record (line);TenRecord >>Info.name; One        while(Record >>word) A Info.phones.push_back (word); - People.push_back (info); -}
1  for(ConstAuto &entry:people)2 {ostringstream formatted, badnums;3       for(ConstAuto &nums:entry.phones)4{if(!valid (nums)) {5Badnums <<" "<<nums;6}Else7Formatted <<" "<<format (nums);8       }9       if(Badnums.str (). empty ())TenOS << entry.name <<" "<< formated.str () <<Endl; One       Else ACerr <<....... -}

"C++primer" Review--with c++11 [3]

Related Article

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.