#include<fstream> // file obj related HRD#include<iostream>intmain(){// ofstream is used for writing files// (1) Init Obj: We'll make a file object called Sample.txtstd::ofstreamoutf{"Sample.txt"};// (2) Error Avoidance: If we couldn't open the output file stream for writingif(!outf){// Print an error and exitstd::cerr<<"Uh oh, Sample.txt could not be opened for writing!\n";return1;}// (3) File Input: We'll write two lines into this fileoutf<<"This is line 1\n";outf<<"This is line 2\n";return0;// When outf goes out of scope, the ofstream// destructor will close the file}
#include<fstream>#include<iostream>#include<string>intmain(){// ifstream is used for reading files// (1) We'll read from a file called Sample.txtstd::ifstreaminf{"Sample.txt"};// (2) If we couldn't open the output file stream for readingif(!inf){// Print an error and exitstd::cerr<<"Uh oh, Sample.txt could not be opened for reading!\n";return1;}// (3) While there's still stuff left to readstd::stringstrInput{};// (3.1) define output streamwhile(inf>>strInput)// (3.2) let file content flood into this output streamstd::cout<<strInput<<'\n';return0;// (4) When inf goes out of scope, the ifstream// destructor will close the file}
#include<fstream>#include<iostream>#include<string>intmain(){// ifstream is used for reading files// We'll read from a file called Sample.txtstd::ifstreaminf{"Sample.txt"};// If we couldn't open the input file stream for readingif(!inf){// Print an error and exitstd::cerr<<"Uh oh, Sample.txt could not be opened for reading!\n";return1;}// While there's still stuff left to readstd::stringstrInput{};while(std::getline(inf,strInput))std::cout<<strInput<<'\n';return0;// When inf goes out of scope, the ifstream// destructor will close the file}
本质上就是把流读入这一步骤进行了修正:
C++
12
inf>>strInput// previousstd::getline(inf,strInput)// fixed now
#include<iostream>#include<fstream>#include<thread>#include<chrono>intmain(){// create fileObject and open filestd::ofstreamoutFile("example.txt");// write datafor(inti=1;i<=5;i++){outFile<<"Line "<<i<<'\n';// use \n rather than endlstd::cout<<"Writing line "<<i<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(1));}// no close(); prog endingstd::exit(0);// data loss}
如果我将std::cout << "Writing line " << i << std::endl;改成std::cout << "Writing line " << i << '\n';会如何?
会不会在CLI中也出现“啥也没有”的现象?
例2:修复上述问题,手动刷新缓冲区
C++
1 2 3 4 5 6 7 8 9101112131415161718192021222324
#include<iostream>#include<fstream>#include<thread>#include<chrono>intmain(){std::ofstreamoutFile("example.txt");for(inti=1;i<=5;i++){outFile<<"Line "<<i<<'\n';if(i==3){// flush at line 3outFile.flush();std::cout<<"Buffer flushed at line 3"<<std::endl;}std::cout<<"Writing line "<<i<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(1));}// outFile.close(); // 正确关闭文件exit(0);// return 0;}
#include<iostream>#include<fstream>#include<thread>#include<chrono>intmain(){// create fileObject and open filestd::ofstreamoutFile("example.txt");// write datafor(inti=1;i<=5;i++){outFile<<"Line "<<i<<'\n';// use \n rather than endlstd::cout<<"Writing line "<<i<<'\n';std::this_thread::sleep_for(std::chrono::seconds(1));}// no close(); prog endingstd::exit(0);// data loss}
#include<iostream>#include<fstream>intmain(){// We'll pass the ios:app flag to tell the ofstream to append// rather than rewrite the file. We do not need to pass in std::ios::out// because ofstream defaults to std::ios::outstd::ofstreamoutf{"Sample.txt",std::ios::app};// If we couldn't open the output file stream for writingif(!outf){// Print an error and exitstd::cerr<<"Uh oh, Sample.txt could not be opened for writing!\n";return1;}outf<<"This is line 3\n";outf<<"This is line 4\n";return0;// When outf goes out of scope, the ofstream// destructor will close the file}
This is line 1
This is line 2
This is line 3
This is line 4
例2: 使用 open() 显式打开文件
就像可以使用 close() 显式关闭文件流一样,也可以使用 open() 显式打开文件流。
open() 的工作方式与文件流构造函数类似 —— 需要一个文件名和一个可选的文件模式。
格式:
C++
1
std::open(filename,mode);
例子:
C++
123456789
std::ofstreamoutf{"Sample.txt"};outf<<"This is line 1\n";outf<<"This is line 2\n";outf.close();// explicitly close the file// Oops, we forgot somethingoutf.open("Sample.txt",std::ios::app);outf<<"This is line 3\n";outf.close();