[CPP] File Stream

File Stream

enter image description here
header file: < fstream>

  • ofstream //output file stream, write things into file
  • ifstream //input file stream, read things to the buffer
  • fstream //read and write the opening file

Opening the file

void open ( const char * filename,  
        ios_base::openmode mode = ios_base::in | ios_base::out );  

open() is a little bit different from normal file open, it has more parameters:
mode:

  • ios::in: read
  • ios::out: write
  • ios::ate: initial position at the end of the file
  • ios::app: all output will be added (appended) to the end of the file. In other words you cannot write anywhere else in the file but at the end
  • ios::trunc: if the file is already exists, then delete the file
  • ios::binary: binary way

According to different use, you can use different mode by “|” symbol.

Write to a file

enter image description here

Note: remember to using namespace std and close the file stream

enter image description here

Read from a file

What we read is the file we create before
enter image description here

评论

此博客中的热门博文

[MLE] Linear Classification

[AIM] MetaHeuristics

[CS231] Neural Networks