[关闭]
@zhengyuhong 2015-06-09T13:01:42.000000Z 字数 2752 阅读 1298

fstream

C++11 C++ STL


ifstream

  1. typedef basic_ifstream<char> ifstream;

ifstream::ifstream

  1. ifstream();
  2. explicit ifstream (const char* filename, ios_base::openmode mode = ios_base::in);
  3. explicit ifstream (const string& filename, ios_base::openmode mode = ios_base::in);
  4. ifstream (const ifstream&) = delete;
  5. ifstream (ifstream&& x);

ifstream::open

  1. void open (const char* filename, ios_base::openmode mode = ios_base::in);
  2. void open (const string& filename, ios_base::openmode mode = ios_base::in)
  1. #include <iostream> // std::cout
  2. #include <fstream> // std::ifstream
  3. int main () {
  4. std::ifstream ifs;
  5. ifs.open ("test.txt", std::ifstream::in);
  6. //std::ifstream ifs("test.txt", std::ifstream::in) 等价于上面两句
  7. char c = ifs.get();
  8. while (ifs.good()) {
  9. std::cout << c;
  10. c = ifs.get();
  11. }
  12. ifs.close();
  13. return 0;
  14. }

ifstream::is_open

  1. bool is_open() const;

Check if a file is open.Returns whether the stream is currently associated to a file.

ifstream::operator=

  1. ifstream& operator= (const ifstream&) = delete;
  2. ifstream& operator= (ifstream&& rhs);

ifstream::operator >>

以空格回车符为分隔符读取数据,

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. int main()
  6. {
  7. ifstream ifs("a.txt");
  8. int n;
  9. while(ifs>>n) {
  10. cout<<n<<endl;
  11. }
  12. ifs.close();
  13. return 0;
  14. }
  15. //当ifs>>n左右数据类型不匹配时会跳出循环,如
  16. //a.txt如果是
  17. //1 2 a b c
  18. //输出2之后就退出循环结束了。
  19. //右侧是stringstream则是另一种情况

  ifstream 流 判断文件是否结尾的函数eof(),我个人理解就是,如ifs>>n,当读入文件最后一个数值,文档的eof,good标志依然是正常的,直到流发现没有下一个数值时才设置eof、good标志。这时候流还是保存上次的值,所以最后可能会读入两次最后的值。如下代码就是读入了两次。

  1. ifstream ifs("a.txt");
  2. int n;
  3. while(ifs.good()) { //while(ifs.eof())
  4. ifs>>n
  5. cout<<n<<endl;
  6. }
  7. ifs.close();

可以如下改写

  1. ifstream ifs("a.txt");
  2. int n;
  3. ifs>>n
  4. while(ifs.good()) { //while(ifs.eof())
  5. cout<<n<<endl;
  6. ifs>>n
  7. }
  8. ifs.close();

如此现读再判断就正常了。个人还是倾向于上面while(ifs>>n)的写法。while判断语句的真实判断对象是ifs,也就是判断当前是否存在有效的输入流。譬如读完最后一个数据后再读时,while(ifs>>n)就会判断错误,因为ifs>>n无法读取数据,是一个无效流,在operator>> API 中清楚看到返回值是流的引用,所以使用while(ifs>>n)判断是否结束读取简单易用。同理使用string中的getline时也可以使用while(getline(ifs,str))很容易就可以判断了。

ifstream::getline

Public member functions inherited from istream

  1. istream& getline (char* s, streamsize n );
  2. istream& getline (char* s, streamsize n, char delim );

getline

getline function in string

  1. istream& getline (istream& is, string& str, char delim);
  2. istream& getline (istream&& is, string& str, char delim);
  3. istream& getline (istream& is, string& str);
  4. istream& getline (istream&& is, string& str);

example

  1. #include <iostream>
  2. #include <string> //std::getline
  3. main ()
  4. {
  5. std::string name;
  6. std::cout << "Please, enter your full name: ";
  7. std::getline (std::cin,name);
  8. std::cout << "Hello, " << name << "!\n";
  9. return 0;
  10. }

ofstream

ofstream::ofstream

  1. ofstream();
  2. explicit ofstream (const char* filename, ios_base::openmode mode = ios_base::out);
  3. explicit ofstream (const string& filename, ios_base::openmode mode = ios_base::out);

mode = std::ofstream::in,out,binary,ate,app

ofstream::operator<<

输出很简单,想输出神马,就ofs<<data就行了。最后关闭输出流。done

  1. #include <fstream> // std::ofstream
  2. int main () {
  3. std::ofstream ofs;
  4. ofs.open ("test.txt", std::ofstream::out | std::ofstream::app);
  5. ofs << " more lorem ipsum";
  6. ofs.close();
  7. return 0;
  8. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注