C ++ I/O operations

Source: Internet
Author: User

------------------------------------------------------------------------------

# Include <iostream>
# Include <fstream>
# Include <istream>
# Include <string>
Using namespace STD;

// Ifstream & open_file (ifstream
& In, const string & file ){
// Here all references are transmitted. Different from value transfer, reference transfer is fast and convenient, and value backup is not performed. On the other hand, it can be ensured
// Modify the parameters at the same time. For value passing, the parameter changes and does not change the real parameter. The reference is acceptable. References that do not want to be changed
// You can add a const to ensure that it is not changed.
//

 
In. Close ();
//
In. Clear ();
//
In. Open (file. c_str ());
// Return
In;
//}
Void main (){

Ifstream
Infile;

Infile. Open ("C: // readme.txt", IOS: In );

If (! Infile)


Cout <"can't open
The file "<Endl;
Fstream
OUTFILE;

OUTFILE. Open ("C: // 1.txt", IOS: Out | IOs: In); // The mode is a file attribute rather than a stream attribute.

String
Str_in;

While (Getline (infile, str_in ))



OUTFILE <str_in <Endl;


Infile. Close ();


OUTFILE. seekg (0, IOS: Beg); // This is very important. Judge the position of the file pointer at all times, or close it first, and then open it again.

//
OUTFILE. Close ();
//
OUTFILE. Open ("C: // 1.txt", IOS: Out | IOs: In); // The mode is a file attribute rather than a stream attribute.
//
While (Getline (OUTFILE, str_in ))

While (OUTFILE. Getline (str_in.begin (), 100, '/N '))



Cout <str_in.c_str () <Endl; // The printed content is empty. Why? It turns out that the file read/write pointer of the OUTFILE above has reached the end of the file, and the pointer needs to be located again in the file header



OUTFILE. Close ();

}
// Conclusion: for historical reasons, the I/O standard library still uses a C-language string instead of a string in C ++, so Str. c_str () conversion is required.

// There are three most important types of I/O operations: iostream is the output of the device, CIN >>, cout <, fstream (ifstream, ofstream ), that is, file operations.

// Getline is the most important thing for ifstream. File Operations are similar to device operations. You can also use ifstream_obj >>, orstream_obj <.

// What I mentioned above is in C ++. For I/O in C, sprintf is more important. formatted output is opposite to atoi, // used to convert a number to a string. I just thought that since we have ITOA, we still need sprintf. Now I want to think about a number that is not an integer.

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.