File I/O and Stream Connections Basics

 
File I/O
 
Objectives
 
Learn input and output streams to work with files
 
Stream Connections
 
Can you use 
cin 
or cout to work with files?
Input stream 
(type: iStream):
 
cin
Output stream 
(type: oStream):
 
cout
We must use some “file streams” to work with files
Data file extension: 
.dat
 
Stream Connections
 
File Stream Basics:
#include <fstream> // short for file stream
Input File Stream type: 
ifstream
Output File Stream type: 
ofstream
 
General Usage Flow
 
Reading in Data from a File
 
When would you know to stop reading from a file?
Utilize an end-of-file ‘marker’
Utilize the ‘state’ of the file stream objects (both ifstream & ofstream)
http://www.cplusplus.com/reference/fstream/ifstream/
End-of-file marker
 
Reading in Data from a File
 
1) Utilize an end-of-file
‘marker’
Input:
 
Reading in Data from a File
 
1) Utilize an end-of-file
‘marker’
Input:
Other string manipulations
for data input can be used
too, e.g. 
getline()
 
Reading in Data from a File
 
1) Utilize an end-of-file
‘marker’
Output:
 
Reading in Data from a File
 
2) Utilize the state of a
file stream object
Input:
 
Reading in Data from a File
 
2) Utilize the state of a
file stream object
Input:
The 
return 
value of 
(fin >> data) 
is
fin 
(recall the overload of  <<)
When there is no more data to read from
the file (or if some errors occurred), the
state of 
fin 
will be evaluated as 
false
 
Review of operator << overload
LHS
RHS
Reference:
return the LHS
 
Reading in Data from a File
 
2) Utilize the state of a
file stream object
Input:
The 
return 
value of 
(fin >> data) 
is
fin 
(recall the overload of  <<)
When there is no more data to read from
the file (or if some errors occurred), the
state of 
fin 
will be evaluated as 
false
After reading in 10, there will
be no more data to read in, so
the state of fin becomes false
 
Reading in Data from a File
 
2) Utilize the state of a
file stream object
Output:
 
Reading in Data from a File
 
Reading in Data from a File
 
Using File Stream Objects as Function Parameter
When used as a function argument, a file stream object (actually any stream object) must be pass-by-reference.
 
Using File Stream Objects as Function Parameter
Making the function call after fin is successfully connected
 
Writing Data to a File
output.dat will be created if it
does not exist
If output.dat exists, then new
data will overwrite the old data
 
Writing Data to a File
How would the output file looks
How would the output file looks
like?
like?
 
Writing (append) Data to a File
 
Append the data written out to the end of an existing file
http://www.cplusplus.com/reference/fstream/ofstream/ofstream/
 
Writing (append) Data to a File
 
Append the data written out to the end of an existing file
http://www.cplusplus.com/reference/fstream/ofstream/ofstream/
How would the output file looks
How would the output file looks
like?
like?
 
One More Note on the Operator << Overload
 
One More Note on the Operator << Overload
This insertion operator overload will work for 
cout
It will also works for the output file stream, so you can use it to
write to a file
Slide Note
Embed
Share

"Learn how to work with file input and output streams in C++. Understand the essentials of connecting file streams, reading data from files efficiently, and utilizing end-of-file markers. Explore examples and best practices for utilizing ifstream and ofstream effectively in your C++ programs."

  • C++
  • File I/O
  • Stream Connections
  • Input Stream
  • Output Stream

Uploaded on Feb 25, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. File I/O

  2. Objectives Learn input and output streams to work with files

  3. Stream Connections Can you use cin or cout to work with files? Input stream (type: iStream): cin Output stream (type: oStream): cout We must use some file streams to work with files Data file extension: .dat

  4. Stream Connections File Stream Basics: #include <fstream> // short for file stream Input File Stream type: ifstream Output File Stream type: ofstream

  5. General Usage Flow 1 Declare and connect a file stream object 2 Use it 3 Close it

  6. Reading in Data from a File When would you know to stop reading from a file? Utilize an end-of-file marker Utilize the state of the file stream objects (both ifstream & ofstream) http://www.cplusplus.com/reference/fstream/ifstream/ End-of-file marker

  7. Reading in Data from a File 1) Utilize an end-of-file marker Input:

  8. Reading in Data from a File 1) Utilize an end-of-file marker Input: Other string manipulations for data input can be used too, e.g. getline()

  9. Reading in Data from a File 1) Utilize an end-of-file marker Output:

  10. Reading in Data from a File 2) Utilize the state of a file stream object Input:

  11. Reading in Data from a File 2) Utilize the state of a file stream object Input: The return value of (fin >> data) is fin (recall the overload of <<) When there is no more data to read from the file (or if some errors occurred), the state of fin will be evaluated as false

  12. Review of operator << overload Reference: LHS RHS return the LHS

  13. Reading in Data from a File 2) Utilize the state of a file stream object Input: The return value of (fin >> data) is fin (recall the overload of <<) When there is no more data to read from the file (or if some errors occurred), the state of fin will be evaluated as false After reading in 10, there will be no more data to read in, so the state of fin becomes false

  14. Reading in Data from a File 2) Utilize the state of a file stream object Output:

  15. Reading in Data from a File

  16. Reading in Data from a File

  17. Using File Stream Objects as Function Parameter When used as a function argument, a file stream object (actually any stream object) must be pass-by-reference.

  18. Using File Stream Objects as Function Parameter Making the function call after fin is successfully connected

  19. Writing Data to a File output.dat will be created if it does not exist If output.dat exists, then new data will overwrite the old data

  20. Writing Data to a File How would the output file looks like?

  21. Writing (append) Data to a File Append the data written out to the end of an existing file http://www.cplusplus.com/reference/fstream/ofstream/ofstream/

  22. Writing (append) Data to a File Append the data written out to the end of an existing file http://www.cplusplus.com/reference/fstream/ofstream/ofstream/ How would the output file looks like?

  23. One More Note on the Operator << Overload

  24. One More Note on the Operator << Overload This insertion operator overload will work for cout It will also works for the output file stream, so you can use it to write to a file

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#