Read and write file operations in C + +

Source: Internet
Author: User

Read and write files this, not commonly used, each time will be baidu, every time the wording is not the same, all always remember Mixed. Use some time today to summarize the works used in previous Projects. After that it was written in this way.

When doing acmicpc like to use Freopen (), This is the use of C language as Follows:

#include <stdio.h>intmain () {freopen ("In.txt","R", stdin); Freopen (" out. txt","w", stdout); intn,m;  while(cin>>n>>M) {cout<<n+m<<endl; }    return 0;}

In this way, multiple groups of n,m are read from the In.txt and then computed n+m are written to out.txt, and each write clears the data from the previous Out.txt.

because, Now internship always write C + +. Here is a read-write operation for C + + Files.

The class library that the file read is written to is fstream, and more specifically Read-ifstream writes are Ofstream

Example:

/************************************************author:guanjuncreated TIME:2017/3/18 13:32:52File Name: 33.cpp*************************************************/#include<bits/stdc++.h>//#include <fstream>using namespacestd;intmain () {ofstream out("D://out2.txt");//initialize an Out objectif(! out{//whether Open cout normally<<"Error"; }    Else{         out<<"1234"; Write data} out. Close (); Closereturn 0;}

hey, c++11 can include all the header files in a <bits/stdc++.h>. The above is under Windows. There is a difference in the Linux path format.

There are times when we do not want to write the second time, before the data is Emptied. Then we can write This.

Example:

/************************************************author:guanjuncreated TIME:2017/3/18 13:32:52File Name: 33.cpp*************************************************/#include<bits/stdc++.h>//#include <fstream>using namespacestd;intmain () {ofstream out("D://out2.txt"Ios:: out|ios::app); if(! out) {cout<<"Error"; }    Else{         out<<"5"; }     out. Close (); return 0;}

In fact, when the Ofstream create the object out of the default ios::out, if you want to open with an additional way, you can add |ios::app, such as ios::app, such as a number of parameters, such as Ios::binary. Be or mean to Be.

File read-in example: read by string, space and line break as split, read to the end of the file

#include <bits/stdc++.h>//#include <fstream>using namespacestd;intmain () {ifstreaminch("D://out2.txt"); if(!inch) {cout<<"Error"; }    Else{        strings;//if It is to determine the type, this block can also be changed to int,float, etc. while(inch>>S) {cout<<s<<endl; }    }    inch. Close (); return 0;}

You can also do this by reading the string as a line, and the delimiter being a newline

#include <bits/stdc++.h>//#include <fstream>using namespacestd;intmain () {ifstreaminch("D://out2.txt"); if(!inch) {cout<<"Error"; }    Else{        strings;  while(getline (inch, S)) {cout<<s<<endl; }    }    inch. Close (); return 0;}

About file write, read out actually have a lot of content, can refer to "c + + Primer Plus 6th edition Chinese version"

About Cin,cout actually have

Here's a wonderful Trick.

How do I replace an integer with a string? can use Ostringstream ah!

Ostringstream os;
os<<123;
String s= "0" +os.str ();
cout<<s<<endl;

Output 0123

Read and write file operations in C + +

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.