0.不要试着从C++编译器的角度理解问题,而是从程序的语义着手。
1.控制台标准输入输出流的使用
1.1IO对象不可复制或者赋值。因此使用IO对象做参数必须使用非const 引用参数(非const因为对IO对象的读写会改变其状态)。
1.2当cin输入非法数据时,fail置为1,当cin遇到文件结尾(ctrl+D)时, fail和eof都为1。
#include <iostream> #include <string> #include <vector> using namespace std; /* *num是int型,当输入字符串时 cin >> num 会失败 * 这种情况下会对num做一个初始化的操作,并且之后cin也不可用 */ int main(int argc, const char *argv[]) { int num; cin >> num ; cout << num << endl; cout << "cin.good = " << cin.good() << endl; cout << "cin.bad = " << cin.bad() << endl; cout << "cin.eof = " << cin.eof() << endl; cout << "cin.fail = " << cin.fail() << endl; cout << "-------------------" << endl; string s; cin >> s; cout << s << endl; cout << "cin.good = " << cin.good() << endl; cout << "cin.bad = " << cin.bad() << endl; cout << "cin.eof = " << cin.eof() << endl; cout << "cin.fail = " << cin.fail() << endl; return 0; }
1.3对于cin 可用的时候为TRUE, 不可用的时候为FALSE。
#include <iostream> #include <string> #include <vector> #include <fstream> #include <stdexcept> using namespace std; /* *文件流对象可用时为true 不可用时为false */ ifstream & open_file(ifstream &is, const string &file){ is.close(); is.clear(); is.open(file.c_str()); return is; } int main(int argc, const char *argv[]) { ifstream is; string filename("a.txt"); if(!open_file(is, filename)){ throw std::runtime_error("file open filed!"); } is.close(); return 0; }
2.文件流的运用
2.1 ifstream 输入文件流(注意这里的输入输出是相对于程序来说的)
2.1.1 读一行显示到终端
#include <iostream> #include <string> #include <vector> #include <fstream> using namespace std; /* *每次从文件中读一行 然后输出到屏幕 */ int main(int argc, const char *argv[]) { ifstream is; is.open("a.txt"); string line; while(getline(is, line)){ cout << line << endl; } is.close(); //文件流最后要关闭 return 0; }
2.1.2 每次读一个单词 存入vector容器中
#include <string> #include <vector> #include <fstream> using namespace std; /* *读取文件中的单词 并存储到vector容器中 */ int main(int argc, const char *argv[]) { string filename = "a.txt"; ifstream is; is.open(filename.c_str()); vector<string> vec; string word; while(is >> word){ vec.push_back(word); } is.close(); cout << "word_num = " << vec.size() << endl; for(vector<string>::iterator it = vec.begin(); it != vec.end(); ++it){ cout << *it << " "; } cout << endl; return 0; }
2.2 ostream 输出文件流
2.2.2 将vector中存储的字符串 写到文件中
#include <iostream> #include <string> #include <vector> #include <fstream> using namespace std; int main(int argc, const char *argv[]) { ofstream os; os.open("out.txt"); vector<string> vec; vec.push_back("hello"); vec.push_back("world"); for(vector<string>::iterator it = vec.begin(); it != vec.end(); ++it){ os << *it << endl; } os.close(); return 0; }
1.2.3 将一个文件的内容拷贝到另外一个文件中去
#include <iostream> #include <string> #include <vector> #include <fstream> using namespace std; int main(int argc, const char *argv[]) { ifstream is; ofstream os; is.open("a.txt"); os.open("b.txt"); string line; while(getline(is, line) ){ os << line << endl; } os.close(); is.close(); return 0; }
3.字符串流
3.1 从文件中读一行存到vector中后,并把一行中的单词存到另一个vector中。
#include <iostream> #include <string> #include <vector> #include <sstream> #include <fstream> using namespace std; int main(int argc, const char *argv[]) { ifstream is; vector<string> lines, words; is.open("a.txt"); istringstream ss; string line, word; while(getline(is, line)){ lines.push_back(line); ss.str(line); // while(ss >> word){ words.push_back(word); } ss.clear(); } is.close(); for(vector<string>::iterator it = lines.begin(); it != lines.end(); ++it){ cout << *it << endl; } cout << "------------------------" << endl; for(vector<string>::iterator it = words.begin(); it != words.end(); ++it){ cout << *it << " "; } cout << endl; return 0; }
0712-----C++Primer听课笔记----------IO流
时间: 2024-11-03 21:37:35