------------------------------------------------------------------------------
MC logo
Read From A File (C++)
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Read From A File (C)] [Read From A File (C++)]
arrecho4.cpp
/*
 * Read some integers into an array, then echo them, using pointers.
 *
 * Author: Tom Bennet
 */
#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;

const int ARRMAX = 100;

int main(int argc, char *argv[])
{
        // See if they specified a file.
        if(argc <= 1) {
                cerr << "Please specify a file name" << endl;
                exit(1);
        }

        // Open the file
        ifstream in(argv[1]);
        if(!in) {
                cerr << "Cannot open %s" << argv[1] << endl;
                exit(1);
        }

        /* Read them in. */
        int arr[ARRMAX];        /* Array of numbers. */
        int *scan = arr;
        while(in >> *scan++);

        /* Print them back out again. */
        cout << "===============" << endl;
        for(int *s = arr; s < scan - 1; )
                cout << *s++ << endl;
}