5 Years
Discussion Span
Last Post by slygoth

You can read about input/output with files here http://cplusplus.com/doc/tutorial/files/

In order to store/read information from a file you have to create an object that represents the file.

This is an example of storing data in a file.

The great thing about file objects is that it's treated as a file stream. Operations you're ordinarily used to using with 'cin' and 'cout' like the '<<' and '>>' operators work the same way. You can insert and extract data from a file stream like you do with 'cin' or 'cout'.

#include <fstream>
#include <iostream>

using namespace std;

int main()
    ofstream output_file;   // create a file for output

    // open the file for output only
    output_file.open("filename_here.txt", ios_base::out);

    if (output_file.is_open()) {    // check if file is opened
        // now let's start inserting data into the file
        output_file << "this is a string\n";
        output_file << "this is another string";

    return 0;

Edited by dx9_programmer

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.