Download presentation
Presentation is loading. Please wait.
1
C++ FileIO pepper
2
fstream fstream From
3
Using fstream Include <fstream> Declare variable of type fstream
Open file – ask your fstream variable to open the file Your ofstream var .open(“filename”,qualifiers) qualifiers In: ios::in Out: ios::out Append: ios::append Binary: ios::binary Ex: ofstream myfile; myfile.open ("example.bin", ios::out | ios::app | ios::binary);
4
Test status of stream Ex: If (myfile.is_open()) {
myfile.is_open() : returns true if open myfile.eof() : returns true if file at end and open for reading myfile.bad() : return true if any read or write fails myfile.good() : opposide of bad Ex: If (myfile.is_open()) { myfile << “ I can write just like cout “;} If (!myfile.eof()) { // read more };
5
Stream positions directions: ios::beg (beginning of file)
Read (Get) Write (Put) Where? tellg tellp Set position seekg (position) seekp (position) Set relative position seekg (offset, direction) seekp (offset,direction) directions: ios::beg (beginning of file) ios::cur (current position) ios::end (end of file)
6
Binary file methods binary read and write helper:
read (buf, size); write(buf, size); Just makes it easier to use a record layout (structure)
7
Close when done Ask your stream to close itself: myfile.close();
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.