Loading

Using STL File I/O

Example 1

// An example of wrting to a text file
#include <fstream>

int main() {
    using namespace std;

    ofstream qFile("ExcerptDOI.txt");
    // An excerpt from the "Declaration of Independence"
    qFile << "We hold these truths to be self-evident, ";
    qFile << "that all men are created equal, that they ";
    qFile << "are endowed by their Creator with certain ";
    qFile << "unalienable Rights, that among these are ";
    qFile << "Life, Liberty and the pursuit of Happiness.";
    qFile.close();
    return 0;
}


Output File:







Example 2

// An example of reading from an text file
#include <fstream>
#include <iostream>
#include <string>

int main() {
    using namespace std;

    // ifstream is an input file
    ifstream qFile("StrongSayNothing.txt");
    // Read in an excerpt Robert Frost's poem
    // "The Strong are Saying Nothing"
    while (qFile.peek() != EOF) {
        string qLine;
        getline(qFile, qLine);
        cout << qLine << endl;
    }
    qFile.close();

    return 0;
}


Input File:





Output:







Example 3

// An example of writing to a binary file
#include <fstream>

int main() {
    using namespace std;

    // Set flags to make this a binary output file
    fstream qFile("Data.dat", ios::out | ios::binary);
    // Write a few ints out to the file
    const unsigned int kuiIntCount = 3;
    int iaArray[kuiIntCount] = {1, 2, 3};
    for (unsigned int uiIndex = 0; uiIndex < kuiIntCount; ++uiIndex) {
        qFile.write((char*)&(iaArray[uiIndex]), sizeof(int));
    }
    qFile.close();

    return 0;
}


Output File:







Example 4

// An example of reading from a binary file
// This example verifies the write in Example 3
#include <iostream>
#include <fstream>

int main() {
    using namespace std;

    // Set flags to make this a binary output file
    fstream qFile("Data.dat", ios::in | ios::binary);
    // Write a few ints out to the file
    while (qFile.peek() != EOF) {
        int iRead;
        qFile.read((char*)&iRead, sizeof(int));
        cout << iRead << endl;
    }
    qFile.close();

    return 0;
}


Input File:





Output:







Example 5

// Example finding the size of a file
#include <iostream>
#include <fstream>

int main() {
    using namespace std;
    // Set flags to make this an input file
    ifstream qFile("ThoseWhoGrant.txt");
    qFile.seekg(0);
    fstream::pos_type qStart    = qFile.tellg();
    qFile.seekg(0, ios::end);
    fstream::pos_type qEnd      = qFile.tellg();
    cout << (qEnd - qStart) << endl;
    qFile.close();

    return 0;
}


Input File:





Output:







Example 6

// An example of copying a file byte by byte
// The original file contains a couplet from
// Alexander Pope's poem "An Essay on Man"
#include <iostream>
#include <fstream>

int main() {
    using namespace std;
    // An input file holding a couplet from
    // Alexander Pope's poem "An Essay on Man"
    ifstream qOriginal("Original.txt");
    ofstream qCopy("Copy.txt");
    char cChar;
    while (qOriginal.get(cChar)) {
        qCopy.put(cChar);
    }

    return 0;
}


Input File:





Output File: