logoISU  

CS256 - Principles of Structured Design

Fall 2021

Displaying ./code/cs256su21code/jul01/readCharFile.c

/*

similar to readChar, but instead of stdin it reads from an external file

*/

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){

	if(argc < 2){
		fprintf(stderr, "%s filename\n", argv[0]);
		exit(1);
	}

	FILE *fp;

	fp = fopen(argv[1], "r");

	if(fp == NULL){
		fprintf(stderr, "Cannot open file\n");
		exit(-1);
	}

	char ch;
	int count = 0;

	while((ch = fgetc(fp)) != EOF){
		count++;
	}

	printf("Number of characters entered: %d\n", count);

	return 0;

}