STL file operations

Source: Internet
Author: User

We often integrate some command programs, such as cout <, CIN>. in actual projects, you will not need to use cout to output data on the screen. there are many operations to read and write files on the hard disk.

How can I read and write files without using MFC or win api.STL provides some classes for you to easily read and write files. There are three common classes:

Fstream: You can write or read files.

Ofstream: Only files can be written.

Ifstream: Only files can be read.

 

Write files

# Include <iostream>

# Include <string>

# Include <fstream> // remember to reference this header file

Using namespace STD;

 

Int main ()

{

// Use fstream to write files

String filepath = "d :\\ arwen.txt ";

Fstream writefile;

Writefile. Open (filepath, IOS: Out); // open the file. If the file does not exist, create the file.

Writefile <"Hello Arwen. "<Endl; // write content to the file. If the file is opened, the content will be overwritten. if you only want to add content to the original content, open the file like this

// Writefile. Open (filepath, IOS: Out | IOs: APP );

Fstream. Close ();

 

// Use ofstream to write files

String filepath = "d :\\ tmp.txt ";

Ofstream filewriteonly;

Filewriteonly. Open (filepath, IOS: Out );

Filewriteonly <"I am Temp File" <Endl;

Filewriteonly. Close ();

 

Read files

// Use fstream to read files

String filepath = "d :\\ arwen.txt ";

Fstream readfile;

Readfile. Open (filepath, IOS: In );

Char ch;

While (readfile. Get (CH ))

Cout <ch;

Readfile. Close ();

 

// Use ifstream to read files

String filepath = "d :\\ tmp.txt ";

Ofstream filereadonly;

Filereadonly. Open (filepath, IOS: In );

While (filereadonly. Get (CH ))

Cout <ch;

 

Filereadonly. Close ();

 

Return 0;

}

 

The above read file is a char read at a time, you can also read a row at a time.

For example

String filepath = "d :\\ tmp.txt ";

Ofstream filereadonly;

Filereadonly. Open (filepath, IOS: In );

Char mystring [100] = {'0 '};

 

While (filereadonly. Getline (mystring, 1000) // The second parameter is the buffer size.

Cout <mystring;

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.