添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
ofstream outFile(char* filename,enum open_mode mode); ifstream inFile(char* filename,enum open_mode mode); open_mode is optional and defined in ios.h for appending, nocreate, noreplace
  • Test if open operation was successful if(!outputFile) // error occurred during open
  • Use << for output and >> for input
  • Output and input operators defined for standard types
  • Useful to provide << for your own classes
    friend ostream& opeator<
  • Use getline() or gets() members to read lines of text.
    inFile.getline(char* buffer,int size,char delimiter); inFile.gets(char** addrOfPtrToChar,char delimiter); With getline() you specify buffer and maximum size. With gets() you specify pointer to a pointer. gets() will provide a buffer with each call and store pointer in your pointer variable. Both functions will read newline from file but not place it in string.
  • Test state of file operations using member functions
    while(!ifstreamObject.eof()) // do something while reading data if(ofstreamObject.bad()) // give error message that file output failed Also good() and fail() available. If stream is not good then all operations are null.
  • There is a position associated with the file. This is the byte location in the file where the next read or write operation will take place.
  • There are functions for manipulating the file position.
    // move to a specific byte position in file inFile.seekg(streampos); outFile.seekp(streampos); // move relative to a direction. seek_dir = beg, cur, end // streamoff is offset from the seek_dir inFile.seekg(streamoff,seek_dir); outFile.seekp(streamoff,seek_dir); // clear eof or bad state inFile.clear(); // find file position streampos inFile.tellg(); streampos outFile.tellg();
  • You can look ahead in an input file or "put back" unwanted characters in the input buffers.
    int inFile.peek(); inFile.putback(char c);
  • There are two in-memory streams that are defined in <strstream.h>. char* p = new char(size); ostrstream outString(char *p, int size); istrstream inString(char *p,int size); All output is written to the string specified in the ostrstream create. Input is read from the istrstream. All stream operations are valid on these streams including stream position and state.
  • Output base - ios::basefield {ios::oct, ios::dec, ios::hex}
  • Output justification - ios::adjustfield {ios::left,ios::right,ios::internal}
  • Floating point format - ios::floatfield {ios::scientific, ios::fixed}
  • Show plus sign - ios::showpos outFile.setf(ios::basefield,ios::hex); outFile.setf(ios::adjustfield,ios::right);
  •