; IO stream input/output streamOfstream Write file stream (out) Buffer->fileIfstream Read file stream (in) File->bufferFStream Read and write file stream (that is, can read and write a certain number of inheritance above the two classes) ostream Standard output streamIStream standard input streamiostream standard input/output stream
; input output stream inheritance diagram
; ofstream Write file stream (text); code#include <fstream>using namespace std; #include <stdio.h> int main (){//output file stream default textchar szbuf1[]= "Hello";Char szbuf2[]= "World";ofstream fout ("F:\\1.txt"); /* if (!fout.is_open ())return 0;logical Inversion If the function returns a result that is not true, it will not really change the value of the returned result return 0 arithmetic inversion ~ Data bitwise inverse RESULTS */if (!fout.is_open ())return 0;//from BUF to Fout output stream Endl commit buffer to filefout<<szbuf1<<endl;fout<<szbuf2<<endl;//fout.flush () Manually submit buffer contents to file//Close pre-commit buffer contents to filefout.close ();return 0;}; ofstream Write file stream (binary); codeofstream fout2 ("F:\\2.txt", ios::out|ios::binary);if (!fout2.is_open ()) {return 0; } //write content to file bufferFout2.write (szbuf1,sizeof (SZBUF1));//Submit buffer contents and close write file streamfout2.close ();; ifstream input file stream file->buffer; streaming Istringstream The meaning of a string as a file, it doesn't work.
C + + Files