[CPP] File Stream
File Stream
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
Note: remember to using namespace std and close the file stream
Read from a file
What we read is the file we create before
评论
发表评论