C++标准输入输出流stream介绍
2017-03-29 13:03
381 查看
This chapter begins by giving you more insight into the features provided by the << and >> operators. It then moves on to introduce the notion of data files and shows you how to implement file-processing applications.
<< >>用于窗口的输入输出。
包含的头文件是istream 和ostream
还有文件的输入输出,包含的头文件是ifstream 和ofstream
下面举一个例子,表示文件的操作。
也可以一次读入一行,那么while循环如下
sstream和string相联系。
可以这样初始化
The following function, for example, converts an integer into a string of decimal digits:
继承关系图
In any event, reading .h files is a programming skill that you will need to cultivate if you want to become proficient in C++.
复习题。
1. 字符,字符串,和文件。
4. 操作符。
<< >>用于窗口的输入输出。
包含的头文件是istream 和ostream
还有文件的输入输出,包含的头文件是ifstream 和ofstream
下面举一个例子,表示文件的操作。
#include <iostream> #include <fstream> #include <string> using namespace std; string promptUserForFile(ifstream &infile, string prompt = "") { while (true) { cout << prompt; string filename; getline(cin, filename); infile.open(filename.c_str()); if (!infile.fail()) return filename; infile.clear(); cout << "Unable to open that file. Try again." << endl; if (prompt == "") prompt = "Input file: "; } } int main(int argc, char *argv[]) { ifstream infile; promptUserForFile(infile, "Input file: "); char ch; while (infile.get(ch)) { cout.put(ch); } infile.close(); return 0; }
也可以一次读入一行,那么while循环如下
string line; while (getline(infile, line)) { cout << line << endl; }
sstream和string相联系。
可以这样初始化
istringstream stream(str);
The following function, for example, converts an integer into a string of decimal digits:
string integerToString(int n) { ostringstream stream; stream << n; return stream.str(); }
int getInteger(string prompt) { int value; string line; while (true) { cout << prompt; getline(cin, line); istringstream stream(line); stream >> value >> ws; if (!stream.fail() && stream.eof()) break; cout << "Illegal integer format. Try again." << endl; } return value; }
继承关系图
void copyStream(istream &is, ostream &os) { char ch; while (is.get()) os.put(ch); }
In any event, reading .h files is a programming skill that you will need to cultivate if you want to become proficient in C++.
复习题。
1. 字符,字符串,和文件。
4. 操作符。
相关文章推荐
- C++的输入和输出与标准输出流
- C++ 标准输入输出流、作用域运算符、注释:
- c++的标准输入输出流
- c++用标准输入输出流测试数据
- C++的标准输入输出流
- C++语法基础--标准IO库--文件的输入和输出(fstream,ifstream,ofstream)
- 七、输入/输出流--IOStreamm基本类和标准IOStream对象--stream状态
- C++ 关于标准输入输出流 20180317 day8
- C++提高 10(标准输入,输出流,文件io流)
- c++ 标准输入输出流基础
- C++ IO流:标准输入输出流的重载
- c++输入输出流介绍
- C++标准输入输出流
- 我对c++输入输出流的理解
- C++对象是怎么死的?关于标准输入输出流(cout,cerr,clog,etc)的进一步探讨
- C++的输入输出流、文件操作
- 标准c++中string类介绍
- C++中的输入输出流
- C++的输入输出流、文件操作
- C++标准头文件结构介绍