------------------------------------------------------------------------------
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.c
/*
 * Read some integers into an array, then echo them, using pointers.
 *
 * Author: Tom Bennet
 */
#include <stdio.h>
#include <stdlib.h>
#define ARRMAX  100
int main(int argc, char *argv[])
{
        int arr[ARRMAX],        /* Array of numbers. */
            numin;              /* Number of integers read. */
        int *scan,              /* Array scanner. */
            *last;              /* Last item read. */
        FILE *in;               /* Input file. */

        /* Open the file. */
        if(argc <= 1) {
                fprintf(stderr, "Please specify a file name\n");
                exit(1);
        }

        if((in = fopen(argv[1], "r")) == NULL) {
                fprintf(stderr, "Cannot open %s\n", argv[1]);
                exit(1);
        }

        /* Read them in. */
        scan = arr;
        while(fscanf(in, "%d", scan++) == 1);
        last = scan - 2;

        fclose(in);

        /* Print them back out again. */
        printf("===============\n");
        for(scan = arr; scan <= last;)
                printf("%d\n", *scan++);
}