C++ For Hackers by

C++ For Hackers by

Language: eng
Format: mobi
Published: 2015-06-01T16:00:00+00:00


05/2013

Lesson 10: File I-O

This is a slightly more advanced topic than what I have covered so far, but I think that it is useful. File I/O is reading from and writing to files. This lesson will only cover text files, that is, files that are composed only of ASCII text.

C++ has two basic classes to handle files, ifstream and ofstream. To use them, include the header file fstream. Ifstream handles file input (reading from files), and ofstream handles file output (writing to files). The way to declare an instance of the ifstream or ofstream class is:

ifstream a_file; or ifstream a_file ( "filename" );

The constructor for both classes will actually open the file if you pass the name as an argument. As well, both classes have an open command (a_file.open()) and a close command (a_file.close()). You aren't required to use the close command as it will automatically be called when the program terminates, but if you need to close the file long before the program ends, it is useful.

The beauty of the C++ method of handling files rests in the simplicity of the actual functions used in basic input and output operations. Because C++ supports overloading operators, it is possible to use << and >> in front of the instance of the class as if it were cout or cin. In fact, file streams can be used exactly the same as cout and cin after they are opened.

#include <fstream>

#include <iostream> using namespace std; int main() {

char str[10]; //Creates an instance of ofstream, and opens example.txt ofstream a_file ( "example.txt" ); // Outputs to example.txt through a_file a_file<<"This text will now be inside of example.txt"; // Close the file stream explicitly a_file.close(); //Opens for reading the file ifstream b_file ( "example.txt" ); //Reads one string from the file b_file>> str; //Should output 'this' cout<< str <<"\n"; // b_file is closed implicitly here

} The default mode for opening a file with ofstream's constructor is to create it if it does not exist, or delete everything in it if something does exist in it. If necessary, you can give a second argument that specifies how the file should be handled. They are listed below:

This will open the file without destroying the current contents and allow you to append new data. When opening files, be very careful not to use them if the file could not be opened. This can be tested for very easily:

05/2013 ios::app -- Append to the file ios::ate -- Set the current position to the end ios::trunc -- Delete everything in the file

For example: ofstream a_file ( "test.txt", ios::app );

ifstream a_file ( "example.txt" ); if ( !a_file.is_open() ) { // The file could not be opened } else {

// Safely use the file stream }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Deep Learning with Python by François Chollet(12569)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Grails in Action by Glen Smith Peter Ledbrook(7696)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Kotlin in Action by Dmitry Jemerov(5062)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3787)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3327)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(3085)
Mastering Bitcoin: Programming the Open Blockchain by Andreas M. Antonopoulos(2867)
The Art Of Deception by Kevin Mitnick(2604)
Drugs Unlimited by Mike Power(2467)
Kali Linux - An Ethical Hacker's Cookbook: End-to-end penetration testing solutions by Sharma Himanshu(2311)
The Innovators: How a Group of Hackers, Geniuses, and Geeks Created the Digital Revolution by Walter Isaacson(2301)
Writing for the Web: Creating Compelling Web Content Using Words, Pictures and Sound (Eva Spring's Library) by Lynda Felder(2261)
SEO 2018: Learn search engine optimization with smart internet marketing strategies by Adam Clarke(2190)
A Blueprint for Production-Ready Web Applications: Leverage industry best practices to create complete web apps with Python, TypeScript, and AWS by Dr. Philip Jones(2150)
JavaScript by Example by S Dani Akash(2136)
DarkMarket by Misha Glenny(2083)
Wireless Hacking 101 by Karina Astudillo(2075)
Full-Stack React Projects by Shama Hoque(1990)