ITP 165 Lecture 12: std_getLine, File I-O

28 views2 pages
ITP 165: Intro to C++ - Lecture 12: std::getLine, File I/O
std::getLine()
In the library, there is a function called std::getline
std::getline allows you to get an entire line of input (so it doesn’t stop until an enter)
std::getline takes two parameters:
The input stream you want to grab the line from (what’s an input stream?!)
The string that you want to store the line in
Stream
A stream is just another word for something that we will either get text input from, or
write text output to
So std::cout is an output stream that writes output to the console
std::cin is an input stream that reads input from the console
(As we’ll see, there are potentially other input and output streams!)
File Input/Output
It needs to read and write to files…we can do this with I/O streams that are specific to
files
So instead of cin/cout, we’ll use different streams
<fstream>
The library handles streams for file input and output
One big difference when using file streams is that unlike cin/cout, you have to declare a
file stream before you use it
This is because the file stream needs to know which file it’s reading in or writing
to
Declaring a file output stream
The syntax for this looks a little weird:
std::ofstream fileStream("output.txt");
“ofstream” stands for “output file stream”
The reason why it looks like this – we are actually instantiating an instance of the
std::ofstream class (also called constructing the object)
File Ouptut Caveat
By default, if a file already exists by the name we specify, it will be overwritten!
So in our case, if we already have a file named “output.txt”, we will lose all of its old
contents
There’s no warning, so be careful not to open a file you don’t want to lose!
is_open
std::ifstream has a member function called is_open that tells us whether or not the file is
open
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Itp 165: intro to c++ - lecture 12: std::getline, file i/o std::getline() In the library, there is a function called std::getline. Std::getline allows you to get an entire line of input (so it doesn"t stop until an enter) The input stream you want to grab the line from (what"s an input stream?!) The string that you want to store the line in. A stream is just another word for something that we will either get text input from, or write text output to. So std::cout is an output stream that writes output to the console. Std::cin is an input stream that reads input from the console. (as we"ll see, there are potentially other input and output streams!) It needs to read and write to files we can do this with i/o streams that are specific to files. So instead of cin/cout, we"ll use different streams. The library handles streams for file input and output.

Get access

Grade+
$10 USD/m
Billed $120 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers