1 / 15

FILE OPEN MODES

FILE OPEN MODES. File open modes. ios::app ios::ate ios::binary ios::in ios::out ios::trunc ios::nocreate ios::noreplace. ios::app. Write all output to the end of file, no matter how you set the seekp pointer. ofstream: open file, write data to the end of file.

Télécharger la présentation

FILE OPEN MODES

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FILE OPEN MODES

  2. File open modes • ios::app • ios::ate • ios::binary • ios::in • ios::out • ios::trunc • ios::nocreate • ios::noreplace

  3. ios::app • Write all output to the end of file, no matter how you set the seekp pointer. • ofstream: open file, write data to the end of file. • ifstream: open file, read data from file. • fstream: open file, write data to the end of file, BUT cannot read data from file. • If the file does not exist, the program will create a new file.

  4. Before Alex Zhao Andrew Nguyen Vincent Tran After Alex Zhao Andrew Nguyen Vincent Tran Tyler Price Example fstream File (“myFile.txt”, ios::app); File.seekp (0);File << “Tyler Price”;

  5. ios::ate • Open a file for output and move pointer to the end of file. Data can be write anywhere in the file. • ofstream: open file, write data to the end of file. • ifstream: open file, but CANNOT read data • fstream: CANNOT open file. • If the file does not exist, the program will create a new file.

  6. Before Alex Zhao Andrew Nguyen Vincent Tran After Tyler PriceAndrew Nguyen Vincent Tran Example fstream File (“myFile.txt”, ios::ate); File.seekp (0);File << “Tyler Price”;

  7. ios::binary • Open a file for binary input and output. • ofstream: open file, discard the file’s content, write data to the file. • ifstream: open file, discard the file’s content. • fstream: CANNOT open file. • If the file does not exist, the program will create a new file.

  8. Before Alex Zhao Andrew Nguyen Vincent Tran After Tyler Price Example ostream File (“myFile.dat”, ios::binary); File .write (interpret_cast <const char *> (&data), sizeof (data));

  9. ios::in • Open a file for input. • ofstream: not support ios::in. • ifstream: open file, read data from file. • fstream: open file, read data from file. • If the file does not exist, the program will create a new blank file.

  10. ios::out • Open a file for output. • ofstream: open file, discard the file’s content, then • write data to the file. • ifstream: not support ios::out. • fstream: open file, discard the file’s content, then • write data to the file. • If the file does not exist, the program will create a new file.

  11. Before Alex Zhao Andrew Nguyen Vincent Tran After Tyler Price Example ostream File (“myFile.txt”, ios::out); File << “Tyler Price”;

  12. ios::trunc • Discard the file’s contents if it exists. • ofstream: open file, discard the file’s content, then • write data to the file. • ifstream: open file, discard the file’s content. • fstream: open file, discard the file’s content, then • write data to the file. • If the file does not exist, the program will create a new file.

  13. ios::nocreate • ofstream: open file, discard the file’s content, then • write data to the file. • ifstream: open file, read data from file. • fstream: cannot open file. • If the file does not exist, the open operation fails.

  14. ios::noreplace • If the file exists, the open operation fails. • ofstream: create a file, then write data to the file. • ifstream: create a blank file. • fstream: cannot open file.

More Related