file open modes file open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n...

Post on 20-Dec-2015

249 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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.

� 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.

Example

BeforeAlex Zhao

Andrew Nguyen

Vincent Tran

AfterAlex Zhao

Andrew Nguyen

Vincent Tran

Tyler Price

fstream File (“myFile.txt”, ios::app);File.seekp (0);File << “Tyler Price”;

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.

Example

BeforeAlex Zhao

Andrew Nguyen

Vincent Tran

AfterTyler PriceAndrew Nguyen

Vincent Tran

fstream File (“myFile.txt”, ios::ate);File.seekp (0);File << “Tyler Price”;

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.

Example

BeforeAlex Zhao

Andrew Nguyen

Vincent Tran

AfterTyler Price

ostream File (“myFile.dat”, ios::binary);File .write (interpret_cast <const char *> (&data),

sizeof (data));

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.

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.

Example

BeforeAlex Zhao

Andrew Nguyen

Vincent Tran

AfterTyler Price

ostream File (“myFile.txt”, ios::out);File << “Tyler Price”;

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.

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.

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.

top related