C++ standard library 2 Flashcards
blank are usually just sequences of bytes stored in your computer’s storage
files are usually just sequences of bytes stores in your computer’s storage
two basic types of files:
text files
binary files
are intended to be interpreted as a string of characters
can be produced and read with programs such as Notepad
text files .txt files .cpp files .py files .html files
are sequences of bytes intended to be interpreted according to a program-defined scheme
Binary files
.docx files
.jpg files
.mp3 files
C++ allows us to work with text files using blank
streams
to use text file must include
Two data types in #include blank
include fstream
#include input and output with file stream
ifstream: input file stream
ofstream: output file stream
ifstream used for
ofstream used for
ifstream used for reading files
ofstream used for writing files
to use file stream
blank them
and call blank with the name of the file you want to open:
example
declare them and call .open( )
ifstream fin;
fin.open(“filename.txt”);
ofstream fout; ;
fout.open(“filename.txt”);
reading and writing files
to read data from an ifstream use blank and blank as you would with cin
use»_space; and getline()
example of ifstream reading data
fin»_space; x»_space; y;
to write data to an ofstream, use blank as you would with cout
«
example of writing data with ofstream
fout «_space;x «_space;” + “ «_space;y «_space;” is “ «_space;x + y;
when you are done, use blank to close the file
.close( )
to ensure the file is properly closed and put away use blank
fin. close( );
fout. close( );
C++ has a header called blank that contains various useful features for many programs
utility
two utilities
pair data type the swap ( ) function
swapping variables
example
**old way
temp = x;
x = y;
y = temp;
**old way
using a blank variable ensures that you don’t lose either value by overwriting it with the other
**old way
temporary value
**old way
C++11 gave new way to swap using blank and blank
include utility and swap ( );
the blank data type allow us to pair together two values of any types
pair data type