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

15
FILE OPEN MODES

Post on 20-Dec-2015

258 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace

FILE OPEN MODES

Page 2: FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace

File open modes

ios::app ios::ate ios::binary ios::in ios::out ios::trunc ios::nocreate ios::noreplace

Page 3: FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n 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.

Page 4: FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace

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”;

Page 5: FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace

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.

Page 6: FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace

Example

BeforeAlex Zhao

Andrew Nguyen

Vincent Tran

AfterTyler PriceAndrew Nguyen

Vincent Tran

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

Page 7: FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace

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.

Page 8: FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace

Example

BeforeAlex Zhao

Andrew Nguyen

Vincent Tran

AfterTyler Price

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

sizeof (data));

Page 9: FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace

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.

Page 10: FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace

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.

Page 11: FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace

Example

BeforeAlex Zhao

Andrew Nguyen

Vincent Tran

AfterTyler Price

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

Page 12: FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace

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.

Page 13: FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace

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.

Page 14: FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace

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.

Page 15: FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace