File Operation Series 2-(Standard C ++ file Stream)

Source: Internet
Author: User

This article describes how to process files in an object-oriented way. However, the disadvantage is that it depends on the operating system and environment. The SDK described later depends on Windows, and cstdiofile depends on the MFC framework language.

The input stream and output stream File class of ifstream and ofstream are used to operate file streams in Standard C ++. To use them, you can directly include the fstream header file. Of course, you can also reference their respective header files as needed.

In fstream, the most important operation is to overload the file input stream operator> and file output stream <operator. Of course, there are many other places for us to use. Example:

Read files:

Char filename [50]; <br/> cout <"pleast enter the file name you want to read:/N"; <br/> CIN> filename; <br/> // ifstream infile (filename, IOS: In); <br/> ifstream infile; <br/> infile. open (filename, IOS: In); </P> <p> // If (! Infile) // You can also <br/> // {<br/> // cout <"file is not exist! "<Endl; <br/> // return; <br/> //} <br/> If (! Infile. is_open () <br/>{< br/> cout <"file is not exist! "<Endl; <br/> return; <br/>}< br/> If (infile. bad () <br/>{< br/> cout <"Open File error! "<Endl; <br/> return; <br/>}< br/> while (! Infile. EOF () <br/>{< br/> char tempbuf [100]; <br/> infile> tempbuf; <br/> cout <tempbuf <Endl; <br/>}< br/> infile. close ();

Write File:

Char filename [50]; <br/> CIN> filename; <br/> ofstream OUTFILE; <br/> OUTFILE. open (filename, IOS: Out); <br/> If (OUTFILE. bad () <br/>{< br/> cout <"Create File error! "<Endl; <br/> return; <br/>}</P> <p> char tempbuf [100]; <br/> while (CIN> tempbuf) <br/> {<br/> If (tempbuf [0] = 'q') <br/> {<br/> break; <br/>}</P> <p> OUTFILE <tempbuf; <br/> OUTFILE <"/N"; <br/>}< br/> OUTFILE. close ();

The Code is also very simple and does not require much explanation. For details, refer to msdn.

The benefits of operating a file using a standard C ++ file stream are also obvious. It is easy to use and efficient, but the danger is still accompanied by these direct operations on the I/O Stream.

Appendix, sample code of this series
The code is tested and passed in vs2008 + xpsp3.

Next article describes file operations with security verification

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.