Using Code to Open a File and Read It Back In - C++ File Stream

C++ examples for File Stream:File Operation

Description

Using Code to Open a File and Read It Back In

Demo Code

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void ReadFile(string filename)
{
    ifstream infile(filename.c_str());// w  ww  .  j av a  2 s. c  o m
    int count;
    int i;
    int num;

    cout << "File: " << filename << endl;
    infile >> count;
    cout << "This file has " << count << " items." << endl;
    for (i=0; i<count; i++)
    {
        infile >> num;
        cout << num << endl;
    }
    infile.close();
}

int main()
{
    ReadFile("../nums1.txt");
    ReadFile("../nums2.txt");
    return 0;
}

Related Tutorials