IOStream
IOstream

What is std::cout?
std::cout?std::cout and std::cin are global objects of classes std::ostream and std::istream respectively, which they've overloaded operator << and >>
is basically this
The same is true for cin
Output stream manipulators
Here the precision is still 4
boolalpha
How does this work?
The ostream object overloads the << operator for each basic type.
The operator returns a reference as seen here basic_ostream<_CharT,_Traits>& which allows combined output
Examples
or
These are some flags
ios::in
Open for input operations.
ios::out
Open for output operations.
ios::binary
Open in binary mode.
ios::ate
Set the initial position at the end of the file.
ios::app
All output operations are performed at the end of the file, appending the content to the current content of the file.
ios::trunc
If the file is opened for output operations and it already existed, its previous content is deleted and replaced by the new one.
Closing the file
When we are finished with our input and output operations on a file we shall close it so that the os is notified and its resources become available again. For that, we call the stream's member function close. This member function takes flushes the associated buffers and closes the file:
Once this member function is called, the stream object can be re-used to open another file, and the file is available again to be opened by other processes.
Lengthy Example (can skip)
file.txt before running
file.txt after running
Here we use ios::app to append the data
Reading
We can get add file name to the the ctor like we did while reading
Sum up numbers
or this
or even a csv file
My exercise - Building an HTML generator
The program will receive a text file using the following syntax:
html.txt
h1 - is and header
p - is a paragraph
img - is an image address
The program will also prompt you for the Websites title
Yeah it took me a while to think about this exercise 😂
TODO add image
Same exercise but now with redirection
Lets remeber some standard redirection first
> redirects standard output of a command to a file, overwriting previous content.
>> redirects standard output of a command to a file, appending new content to old content.
< redirects standard input to a command.
html.txt
Notice here the title (the first line) doesn't have a key
Here we will use the < redirection operator
The main differences are the following rows:
Now we are using cin
Some more redirection
Lets change our interface to something like this
Our programs
input is now html.txt
and it outputs to website.html
Now we have
Some other things we could do is add ios::app to our output stream or some other flag
Serializing Objects
Lets try to serialize a student object
Read here if we would like to serialize this in binary format
Binary files
Input/output with files - C++ Tutorials
get and put stream positioning
All i/o streams objects keep internally -at least- one internal position:
ifstream, like istream, keeps an internal get position with the location of the element to be read in the next input operation.ofstream, like ostream, keeps an internal put position with the location where the next element has to be written.
Finally, fstream, keeps both, the get and the put position, like iostream
seekg() and seekp()
seekg() and seekp()These functions allow to change the location of the get and put positions
seekg ( position );seekp ( position );
or
seekg ( offset, direction );seekp ( offset, direction );
tellg() and tellp()
tellg() and tellp()These two member functions with no parameters return a value of the member type streampos, which is a type representing the:
current get position (in the case of tellg)
the put position (in the case of tellp).
Another example:
Lets say we would like to Serializing & Deserializing Objects
More advanced example
write() We need to store the data from the string along with the size because to restore it we need to temporarily read it somewhere before storing it in the std::string (istream::read() doesn't read directly to std::string)
read() When we read the string data we need somewhere to store it because we std::string isn't a primitive type. So we
read the size
allocate an array
read the data into the array,
load the std::string
and delete the array
Lets explain the write():
1st we save/write the figure which contains the name and type
2nd we save each and every point
SFML version

TODO fix genertaion of triangles
Add Fighter and Ninja
Last updated