Stream class libraries and files

Source: Internet
Author: User

1. Stream Class Library

(1), there is no input and output in C + +, the standard library contains an I/O Stream class library.

printf and scanf are called functions in the C language; output to screen

Cout and Cin in C + + are called objects; input is a keyboard

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/85/3C/wKiom1edvmuwrDK6AAActDZs2yI853.png-wh_500x0-wm_3 -wmp_4-s_3367844552.png "title=" Qq20160731170112.png "alt=" Wkiom1edvmuwrdk6aaactdzs2yi853.png-wh_50 "/>

(2), 4 global stream objects defined in C + + Stream class library, CIN, cout, Cerr, clog

CIN standard input Stream object, keyboard

cout standard output stream, screen

Cerr and clog standard error output stream, screen

Where CIN, cout, clog are buffers, buffers are managed by Streambuf class objects, cerr non-buffers, and appear immediately after an error occurs.

#include <iostream>using namespace Std;int main (void) {cerr<< "error" <<endl;//Error Direct output cout<< "H Ello "<<endl; Put it in the buffer first}

2. Format Control

will use, know how to check on the line, there is no need to remember these;

Cout.flags (Ios::hex); Hex these are in iOS class, with 16 binary output;

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/85/3C/wKiom1edxsHDHmcEAAA_d6rPdCg046.png-wh_500x0-wm_3 -wmp_4-s_4028720849.png "title=" Qq20160731173650.png "alt=" Wkiom1edxshdhmceaaa_d6rpdcg046.png-wh_50 "/>

0000 0000 0000 0000 How many 1, what function;

Ios::hex | Ios::showbase Hex and Showbase are all enumerations defined in the iOS class, and each of the 1-16 accounts for one;

3. Documents

(1), first define a file object flow

(2). Open a file

(3), to read and write files

(4), close the file

Operation of the text file, writing to the file:

 #include <iostream> #include <fstream>//file output stream header file #include< Stdlib.h>using namespace std;int main (void) {    int ar[] = &NBSP;{1,&NBSP;3,&NBSP;5,&NBSP;6,&NBSP;7,&NBSP;9,};&NBSP;&NBSP;&NBSP;&NBSP;//1,     // Ofstream ofile ("Test1.txt",  ios::out), and the next 2 steps equivalent     ofstream ofile;   &NBSP;&NBSP;//2,     ofile.open ("Test1.txt",  ios::out);     if (! ofile) {        cerr<< "open file fail!" <<endl;        exit (1);    }     for (int i = 0; i < sizeof (AR)/sizeof (int);  i++) {         ofile<<ar[i]<< " ";    }     ofile.close ();} 

Read in the file:

#include <iostream> #include <fstream>//file output stream header file #include<stdlib.h>using namespace Std;int main (    void) {int ar[10];    Ifstream ifile;    Ifile.open ("Test1.txt", ios::in);        if (!ifile) {cerr<< "Open File Fail" <<endl;    Exit (1);    } for (int i = 0; i < 4; i++) {ifile>>ar[i]; } ifile.close ();}

4. Binary read/write

Write file:

#include <iostream> #include <fstream>//file output stream header file #include<stdlib.h>using namespace Std;int main (    void) {int ar[] = {1, 3, 5, 6, 7, 9,};    1,//ofstream ofile ("Test1.txt", Ios::out), and the next 2 steps equivalent ofstream ofile;    2, Ofile.open ("Test1.txt", Ios::out | ios::binary); if (!ofile) {cerr<< "Open File fail!"        <<endl;    Exit (1); } ofile.write (char *) AR, sizeof (AR));//This does not cycle, one-time solution ofile.close ();}

Read from a file:

#include <iostream> #include <fstream>//file output stream header file #include<stdlib.h>using namespace Std;int main (    void) {int ar[] = {1, 3, 5, 6, 7, 9,};    1,//ifstream ofile ("Test1.txt", ios::out);    Ifstream ifile;    2, Ifile.open ("Test1.txt", Ios::out | ios::binary); if (!ifile) {cerr<< "Open File fail!"        <<endl;    Exit (1); } ifile.read (char *) AR, sizeof (AR));//This does not have to cycle, all at once read out Ifile.close ();}

5. Random File access

Random Read and write key by file pointer;

         file pointer, start pointing to the first, read and write on this file read and write pointers, will automatically point to the next;

#include <iostream> #include <fstream>//file output stream header file #include<stdlib.h>using namespace std ; Int main (void) {    ifstream ifile;    ifile.open ("Test1.txt ",  ios::in);     if (!ifile) {        cerr< < "open file fail!" <<endl;        exit (1);    }     int pos;    int value;    while (1) {         cout<< "Please enter position: ";         cin>>pos;        //beg  cur  end  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IFILE.SEEKG (Pos, ios::beg);//positioning function, POS, relative to where to start          ifile>>value; //Place the value of the location into the Value;        cout<< "value = " <<value<<endl;    }     ifile.close ();}

Files can be positioned to read out, preferably in binary resolution, each number is 4 bytes, do not consider each number across several bytes;

text files in which each digit (0-9) occupies 1 bytes, which is not good for locating a full number that occupies a few bytes;

5. Files and objects

In C + + programming, the file should be opened in the constructor, and create objects, and in the destructor to save and close the file, and revoke the object;

In the case of a file, releasing the resource includes storing the information in the object again in the disk file, and in the process of running the program, the information should be saved to the appropriate

Disk files so that data is not accidentally lost.

The organic combination of the file and the object (key in the construction and destructor), here is a corresponding example:

#include <iostream> #include <fstream>using namespace std;class Complex;ostream&  operator<< (ostream &out, const complex &t);class complex{     friend ostream& operator<< (ostream &out, const  complex &t);p Ublic:    complex ()  : real (0),  image (0) {         ifstream ifile;         Ifile.open ("Test.txt",  ios::in);         if (!ifile) {             cerr<< "Open file fail" <<endl ;             exit (1);         }        ifile>>real>>image;       &nbsP; ifile.close ();     }    complex (int rea, int  IMAG)  : real (REA),  image (imag) {}    ~complex () {         ofstream ofile;        ofile.open (" Test.txt ",  ios::out);         if (!ofile) {             cerr<< "Open file fail" <<endl;             exit (1);         }        ofile<<real<< " " <<image;         ofile.close ();    }public:     void setcomplex (int real, int image) {         this->real = real;        this->image = image;    } Private:    int real;    int image;};o stream& operator<< (ostream &out, const complex &t) {     out<< "(" <<t.real<< "," <<t.image<< ")";     return  out;} Int main (void) {    complex c;    cout<<c<<endl;     c.setcomplex (100, 200);//simulates the last read of the next instantiation.     cout<<c<<endl;//write into the file, save the settings. }

Stream class libraries and files

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.