🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #647

FILE READ PROBLEM

#647
From: GREG HOOPER
Date: Sat, 15 Nov 1997 00:00
102 lines
3053 bytes
Below is a program that achieves the correct end result that I'm after. The
problem is 
I'm only allowed to read the text file once.
Can anyone show me how to achieve this to give the same output that I have.

Any help would be appreciated Thanks  GREG   :-(

/* PURPOSE OF PROGRAM */
/*
Read contents of a text file once via re-direction
character by character, process the characters
read and display a simple horizontal bar graph
that depicts the frequency of the first five
characters found in the text file.
*/

/* LIBRARY */
#include <stdio.h>

/* FUNCTION CALLS */
void NumOfChars(char[], int, int[]);
void CharPrint(char[], int[]);
void AstPrint(int[], int);
void pause(void);
void main()
	{
	int NumArray[] = {0,0,0,0,0}, get_char, i = 0;
	char CharArray[] = {0,0,0,0,0};

	do
		{
		get_char = getchar();

		if ((get_char > 64 && get_char < 91) || (get_char > 96 && get_char <
123))
			{
				CharArray[i] = get_char;
				i++;
			}
		}while((CharArray[i] != EOF));

	 while(get_char != EOF)
		{
			get_char = getchar();

			if ((get_char > 64 && get_char < 91) || (get_char > 96 && get_char <
123))
				NumOfChars(CharArray, get_char, NumArray);
		}

	 printf("\nASCII Char	Frequency");
	 printf("\n------------------------------------------------------------");
	 CharPrint(CharArray, NumArray);
     
	 pause();
}
/* FUNCTION -  count number of matching chars to CharArray */
void NumOfChars(char CharArray[], int get_char, int NumArray[])
	{
	int pos;

		for(pos = 0; pos < 5; pos++)
			{
				if(CharArray[pos] == get_char)
					(NumArray[pos]) += 1;
			}
	}

/* FUNCTION - incremental call to character position in array
with call to AstPrint function in loop.
*/
void CharPrint(char CharArray[], int NumArray[])
	{
	int pos;

		for (pos = 0; pos < 5; pos++)
			{
				printf("\n%10c\t", CharArray[pos]);
				AstPrint(NumArray, pos);
			}
	}
/* FUNCTION - prints no. of "*" for position value of NumArray */
void AstPrint(int NumArray[], int pos)
	{
	int counter;

		for(counter = 0; counter < NumArray[pos]; counter++)
			printf("*");
	}

/* pause function */
void pause(void)
	{
	int hold;
		fflush(stdin);
		printf("\n\n PAUSED, Press Enter To Finish.\n");
		scanf("%c", hold);
	}
   
-- 
comp.lang.c.moderated - clcm@plethora.net
Technical Details
Message-ID: <clcm-19971114-0010@plethora.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fab7a,75d4be5da465827f,start
X-Google-Attributes: gidfab7a,public
X-Google-Thread: 100850,75d4be5da465827f,start
X-Google-Attributes: gid100850,public
X-Google-Thread: 114592,75d4be5da465827f,start
X-Google-Attributes: gid114592,public
From: "GREG HOOPER" <upgrade@b022.aone.net.au>
Subject: FILE READ PROBLEM
Date: 1997/11/15
Message-ID: <clcm-19971114-0010@plethora.net>
X-Deja-AN: 289671607
NNTP-Posting-Date: 14 Nov 1997 23:08:09 CST
Originator: clcm@plethora.net (comp.lang.c.moderated)
Organization: Customer of Access One Pty Ltd, Melbourne, Australia
Newsgroups: alt.comp.lang.learn.c-c++,comp.lang.c.moderated,ctdl.lang.c