0% found this document useful (0 votes)
6 views9 pages

File IO

The document provides an overview of file I/O in C++, focusing on streams for reading and writing data to and from files. It explains the different types of file streams (ifstream, ofstream, fstream) and their usage, as well as how to handle file paths and errors. Additionally, it includes code examples for reading from and writing to files, along with error handling techniques.

Uploaded by

harisamser27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

File IO

The document provides an overview of file I/O in C++, focusing on streams for reading and writing data to and from files. It explains the different types of file streams (ifstream, ofstream, fstream) and their usage, as well as how to handle file paths and errors. Additionally, it includes code examples for reading from and writing to files, along with error handling techniques.

Uploaded by

harisamser27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

FILE I/O

STREAMS

This Photo by Unknown Author is licensed under CC BY-SA


WHAT ARE STREAMS?
• Define the flow of data from input to output
• Built-in C++ stream classes
• Console I/O – Between program and interface devices
• iostream, istream, ostream, streambuf
• Disk I/O – Between program and files
• fstream, ifstream, ofstream, filebuf
FILE STREAMS
• Read and write to files of various formats
• ifstream – Input file stream (read)
• ofstream – Output file stream (write)
• fstream – Simultaneous read/write
• filebuf – File read/write buffer
ACCESSING AND READING FILES
FILE PATHS (SYNTAX VARIES BY
OPERATING SYSTEM)

Absolute Path Relative Path


• Begins at root director (C:/) • Begins at working directory
• Represents complete address in • Represents path from working
storage directory to destination
• C:/Users/John/Documents/Taxes/2024/W- • /Taxes/2024/W-2.pdf
2.pdf
READING FROM A FILE
• Include the fstream library #include <iostream>
#include <fstream>
• Create these variables: #include <string>
using namespace std;
• ifstream object
• string for file path int main(){
ifstream fin;
• string to hold a line taken from the string filename = "slideCode.py";
file string line;
fin.open(filename);
• Call ifstream.open(path) while(getline(fin, line)){
cout<< line << endl;
• Iterate over file line by line }
fin.close();
• When done, call ifstream.close() return 0;
}
WRITING TO A FILE
• Include the fstream library int main(){
ofstream fout;
• Create these variables: string filename = "todo.txt";
• ofstream object string line = "";
• string for file path fout.open(filename);
while(line != "-1"){
• string to hold a line of input data
cout<< "Enter a task: ";
• Call ofstream.open(path) getline(cin, line);
if(line != "-1"){
• Iterate over input line by line fout<< line << endl;
• Use fout to write input to file }
}
• When done, call
fout.close();
ofstream.close() return 0;
}
HANDLING ERRORS
• An empty stream evaluates to int main(){
false ifstream fin;
string filename = "slideCode.py";
• Check after opening file string line;
fin.open(filename);
• If false, raise an error if(!fin){
• Can also be handled using cerr<< "ERROR: Could not open file." <<
endl;
exceptions return 1;
}
while(getline(fin, line)){
cout<< line << endl;
}
fin.close();
return 0;
}

You might also like