Walk through the c++ process: input and output streams

flow

meaning of flow

Flow is an abstract concept, which can be understood as a turbulent river as a carrier for transmitting data. There are only a few places where data can be stored, such as the data just entered from the keyboard, the data in the memory, the data in the disk file, etc. The flow of these data to each other makes the concept of flow come into being. By now you should understand that he is a carrier for transmitting data.

I/O stream

The input and output stream contains a lot of functions, such as cin.getline() cin.get()    
too much, just say cout, cin

The difference between c and c++

    1. As an introductory course, c language is the first choice. Anyone who has learned c knows that the output and input of c are printf and scanf. The biggest advantage is accuracy. As long as you are careful enough, your c language code can be regarded as the best language .
    2. But not everyone is so careful. For these people, precision becomes a disadvantage. When you use %d to output super precision or string numbers, the compilation system will think it is correct (why The compilation system can’t be set up, can it be recognized? This is about the principle of composition, let’s talk about it at the end, I won’t talk about it here), and an error occurs when it is running, which is very troublesome.
    3. C++ retains the input and output of c on the one hand, and has its own input and output cin and cout on the other. This kind of input and output stream is much more convenient to use. Write the input variable name after cin>> or write the output variable name after cout<<

Advantages of C++ I/O

This has many advantages, and there will be no wrong input and output without error reporting, so it is type-safe . C++ is object-oriented, so the input and output stream can output objects, and object-orientation is an advantage. As you can know in other chapters, c++ can overload symbols, of course, it can also overload >> and <<, so its functions can be changed at any time, and scalability is an advantage. In fact, these advantages are also the advantages of C++.

I流

Java's io stream library is very large. Similarly, C++'s stream library is also very powerful. Let me introduce the stream library

IO stream library

insert image description here
iostream input and output stream base class
istream output stream base class
ostream output stream base class

fstream input and output file stream class
ifstream input file stream class
ofstream output file stream class

file input and output stream

File stream, the main operation is the file in the disk. As can be seen from the above, many header files can be used. Generally speaking, using this on the header file is almost enough

#include<fstream>

The way to open the file
ios::in Open the file as input
ios::out Open the file as output, if the file already exists, clear the file
ios::app Open the file as output, add
ios::ate at the end to open An existing file, the file pointer points to the end of the file
ios::trunc  
ios::binary Open the file in binary mode
ios::in|ios::out Open the file in input and output mode, the file can be read and written
ios::out| ios::binary Open an output file in binary mode
ios::in|ios::binary Open an input file in binary mode

character stream

    In c++, this is not particularly clear, but in java, it is very clear. I have learned java, so it is easy to remember by myself.
    A character stream file is a file whose content uses Unicode encoding. To put it simply, if you open it with Notepad and you can recognize it, then this is a character stream file. The storage method of the disk is indeed based on the byte as the basic unit, so the character stream file storage is to convert the file into bytes before storing it on the disk. When character stream files are processed, there is always a Unicode conversion process in the middle, and the speed will slow down.
    To read and write this kind of file, use ofstream outfile("file name", open mode), generally it doesn't matter if the open mode is not binary.

#include<fstream>
using namespace std;
int main(){
    
    
//文件内容输入
	ostream outfile("我是文件.txt",ios::out);
	outfile<<"这是我要存入文件的内容";
//文件内容输出
	ifstream infile("又是文件.txt",ios::in);
	//文件有很多字?那用个数组和循环吧
	char a[100];
	for(int i=0;i<100;i++){
    
    
		infile>>a[i];
	}
	return 0;
}

Many people find that out is actually input, and in is actually output, which is difficult to remember. In fact, it is good to regard the subject as memory. When encountering the input and output of character stream files, the main body is regarded as memory, out outputs data from the memory, and reaches the file, and in is to input data from the file to the memory, remembering the memory, and clearing the file output and input (If the above notation feels messy, just ignore it)

byte stream

The byte stream file is straight from the memory to the disk file, so the speed is faster. Of course, the binary opening method needs to use Ios::binary. To read and write files, you need to use the write and read methods

#include<fstream>
using namespace std;
int main(){
    
    
	ofstream outfile("又是我.dat",ios::binary);
	outfile.write("你好你好,我又要进去了",10);//后面那个是前面字符串的长度
	
	char *a;
	istream infile("又是我.dat",ios::binary);
	infile.read(a,sizeof(a));

	return 0;
}

The file class pointer has been opened and used up, and must be closed with close() at the end

Write a program that uses the above class


/*************************************************************************
        > File Name: filefunction.cpp
        > Author:wangzhiyu12
        > Mail:
        > Created Time: 2020年07月04日 星期六 17时12分37秒
 ************************************************************************/

#include<iostream>
#include<stdlib.h>
#include<fstream>
#include<string>
using namespace std;

//字符流 文件输入
void character_file_out(char filename[]){
    
    
    ofstream outfile(filename,ios_base::out|ios_base::in);
    if(!outfile){
    
    
        cerr<<"open error"<<endl;   //标准错误信息输出关键字cerr
        exit(1);                    //若出错,退出
    }
    char ch;
    while((ch=cin.get())!=EOF){
    
    
       // cout<<ch;
        outfile<<ch<<" ";
    }
}

//字符流文件 读出,显示控制台
void character_file_input(char filename[]){
    
    
    ifstream infile(filename,ios_base::in|ios_base::out);
    if(!infile){
    
    
        cerr<<"open error!"<<endl;
        exit(1);
    }
    char ch;
    while(infile.get(ch)){
    
          //一个字符一个字符的获取文件内容,并且赋给ch
        cout.put(ch);           //输出ch
    }
}

void byte_file_out(char filename[]){
    
    
    fstream outfile(filename,ios_base::binary);
    if(!outfile){
    
    
        cerr<<"open error";
        exit(1);
    }
    char *ch;
        while((ch=cin.get())!=EOF){
    
    
        outfile<<ch<<" ";
    }
}

//字符流文件 读出,显示控制台
void character_file_input(char filename[]){
    
    
    ifstream infile(filename,ios_base::in|ios_base::out);
    if(!infile){
    
    
        cerr<<"open error!"<<endl;
        exit(1);
    }
    char ch;
    while(infile.get(ch)){
    
          //一个字符一个字符的获取文件内容,并且赋给ch
        cout.put(ch);           //输出ch
    }
}

void byte_file_out(char filename[]){
    
    
    fstream outfile(filename,ios_base::binary);
    if(!outfile){
    
    
        cerr<<"open error";
        exit(1);
    }
    char *ch;
    cin.getline(ch,10,'\n'); //获取一行
    outfile.write(ch,sizeof(ch));
}

void byte_file_input(char filename[]){
    
    
    fstream infile(filename,ios_base::binary);
    if(!infile){
    
    
        cerr<<"open error"<<endl;
        exit(1);
    }
    char *ch;
    infile.read(ch,sizeof(ch));
    cout<<ch;
}

int main(){
    
    
	//调用函数

    return 0;
}

The above program is written in vim in linux, and ios_base can be changed to ios

Guess you like

Origin blog.csdn.net/wangzhiyu12/article/details/107117163