C + + initial stage - I/O flow

catalogue

1, Input and output of C language

2, C++I/O flow

C++I/O flow

C + + standard I/O flow

C + + file I/O stream

stringstream character stream

1, Input and output of C language

  • scanf() reads data from the standard input device and stores the value in the variable;
  • printf(), match the specified text / string output to the standard output device, and pay attention to the control of output width and accuracy;

C language carries out input and output with the help of corresponding cache:

  • The buffer can shield the implementation of low-level I/O. the implementation of low-level I/O depends on the implementation of the kernel of the operating system itself. If this difference can be shielded, it is easy to write a portable program;
  • This part can be used to realize the "row" reading behavior. The computer has no probability of "row". With this part, the concept of "row" can be defined, and then the contents of the buffer can be parsed to return a "row";

2, C++I/O flow

  • Stream is an abstract description of an ordered, continuous and directional data (the unit can be bit/byte/packet);
  • C + + stream refers to the process of information input from external input device to computer (such as memory) and output from memory to external device;
  • Characteristics: orderly, continuous and directional;
  • In order to realize this flow, C + + defines the I/O standard class library, which is called flow or flow class;

C++I/O flow

  • The C + + system implements a huge class library, in which ios is the base class, and other classes are directly or indirectly derived from ios classes;

C + + standard I/O flow

  • The C + + standard library provides four global flow objects, cin, cout, cerr and clog;
  • The header file must be included and the std standard namespace must be introduced;
  • cin, standard input, that is, data is input into the program through the keyboard;
  • cout for standard output, that is, data flows from memory to console (display);
  • cerr, output standard error;
  • Log to output the log;

Note:

  • cin is the buffer stream, and the data input by the keyboard is saved in the buffer. When it is to be extracted, it is taken from the buffer; If there are too many inputs at one time, it will be left for slow use. If there is an input error, it must be modified before entering. New data is required only after the data in the input buffer is fetched;
  • The input data type must be consistent with the data type to be extracted, otherwise an error occurs. The error only corresponds to position 1 in the flow status word state, and the program continues;
  • Space and carriage return can be used as separators between data, so multiple data can be entered in one line or in separate lines; However, if the time character type and character string are used, the space (ASCII code is 32) cannot be input with cin, and the carriage return character cannot be read;
  • Built in type data can be directly input and output, because the standard library has overloaded the input and output of all built-in types; For user-defined types, you need to overload <, > >;

For I/O algorithms, cyclic input is generally required:

//Single element cyclic input
int a;
while (cin >> a)
{
	cout << a << " ";
}
//Multiple element cyclic input
int a, b, c;
while (cin >> a >> b >> c)
{
	cout << "a:" << a << endl;
	cout << "b:" << b << endl;
	cout << "c:" << c << endl;
}
string str;
getline(cin, str)
cout << "str:" << str << endl;

C + + file I/O stream

  • According to the data format of file content, it can be divided into binary file and text file;

General steps for operating files with file stream objects:

  • Define a file stream object, ifstream ifile (for input only), OFSTREAM ofile (for output only), fsstream iofile (for both input and output);
  • Use the member function of the file stream object to open a disk file to establish continuity between the file stream object and the disk file;
  • Use extraction and insertion operators to read and write files, or use member functions to read and write files;
  • Close the file;
// Use file IO stream to demonstrate reading and writing configuration files in text and binary mode
#include<string>
#include<fstream>
using namespace std;

struct ServerInfo
{
	char _ip[32];
	int _port;
};

struct ConfigManager
{
public:
	ConfigManager(const char* configfile = "server.config")
		:_configfile(configfile)
	{}

	void WriteBin(const ServerInfo& info)
	{
		//Binary open and write
		ofstream ofs(_configfile, ofstream::out | ofstream::binary);
		ofs.write((const char*)&info, sizeof(ServerInfo));
		ofs.close();
	}

	void ReadBin(ServerInfo& info)
	{
		//Binary open and read
		ifstream ifs(_configfile, ifstream::in | ifstream::binary);
		ifs.read((char*)&info, sizeof(ServerInfo));
		ifs.close();
	}

	void WriteText(const ServerInfo& info)
	{
		ofstream ofs(_configfile);
		ofs << info._ip << endl;
		ofs << info._port << endl;
		ofs.close();
	}

	void ReadText(ServerInfo& info)
	{
		ifstream ifs(_configfile);
		ifs >> info._ip;
		ifs >> info._port;
	}

private:
	string _configfile;
};

int main()
{
	ConfigManager CM;
	ServerInfo rinfo;
	ServerInfo winfo;
	strcpy(winfo._ip, "120.0.0.1");
	winfo._port = 1;
	
	//Binary read / write
	CM.WriteBin(winfo);
	CM.ReadBin(rinfo);
	//Text reading and writing
	CM.WriteText(winfo);
	CM.ReadText(rinfo);

	return 0;
}

stringstream character stream

  • When using stringstream, the package header file must be;
  • Istringstream (input stream), ostringstream (output stream), iostringstream (input / output stream);
  • It is mainly used to format numeric type data into string and string splicing;

Note:

  • stringstream actually maintains a string type object at its bottom to save the results;
  • During multiple data type conversions, clear() must be used to clear the data before correct conversion. However, clear() will not empty the string object at the bottom of stringstream; At the end of the stringstream transformation (i.e. after the last transformation), the internal state will be set to badbit, so the next transformation must call clear() to set the state to goodbit;
  • You can use the s.str("") method to set the underlying string object to an empty string; Otherwise, multiple conversions will accumulate all the results in the underlying string object;
  • You can use s.str() to return the string stream to its underlying string object;
  • stringstream uses string class objects instead of character arrays to avoid the danger of buffer overflow, and it will deduce the parameter types without formatting control and the risk of formatting failure. Therefore, it is more convenient and safe to use;
int main()
{
	int a = 12345678;
	double b = 123.456;
	string sa;

	stringstream s;
	s << a;
	s >> sa;

	s.str("");
	s.clear();

	s << b;
	s >> sa;

	string sValue;
	sValue = s.str();
	return 0;
}
int main()
{
	stringstream sstream;

	//Put multiple strings into ssstream
	sstream << "fiste" << " " << "string,";
	sstream << " second string;";
	cout << sstream.str() << endl;

	//Empty ssstream
	sstream.str("");
	sstream << "third string;";
	cout << sstream.str() << endl;
	return 0;
}

Tags: C++ Programming

Posted on Fri, 03 Dec 2021 14:17:37 -0500 by rcmehta_14