Download presentation
Presentation is loading. Please wait.
2
FILE OPEN MODES
3
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
4
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.
5
Example Before Alex Zhao Andrew Nguyen Vincent Tran After Alex Zhao Andrew Nguyen Vincent Tran Tyler Price fstream File (“myFile.txt”, ios::app); File.seekp (0); File << “Tyler Price”;
6
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.
7
Example Before Alex Zhao Andrew Nguyen Vincent Tran After Tyler PriceAndrew Nguyen Vincent Tran fstream File (“myFile.txt”, ios::ate); File.seekp (0); File << “Tyler Price”;
8
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.
9
Example Before Alex Zhao Andrew Nguyen Vincent Tran After Tyler Price ostream File (“myFile.dat”, ios::binary); File.write (interpret_cast (&data), sizeof (data));
10
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.
11
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.
12
Example Before Alex Zhao Andrew Nguyen Vincent Tran After Tyler Price ostream File (“myFile.txt”, ios::out); File << “Tyler Price”;
13
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.
14
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.
15
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.