ive made a simple program to read the number of characters entered from the standard input.
i was wondering if someone could help me tweak the program to read from a file instead of from the standard input.
help plz!
Code:
#include <stdio.h>
#include <string.h>
#define MAX_CHARS 20
int GetStr(char string[], int maxLen);
int main(void)
{
int stringLength;
char string[MAX_CHARS + 1] = {'\0'};
printf("Please enter a string:\n");
stringLength = GetStr(string, MAX_CHARS);
printf("Length of string is: %d\t",stringLength);
return(0);
}
/*****************************************************************
//
// GetStr Function definition
//
// This function reads a series of characters from the user until
// MAX_CHARS is reached.
//
/*****************************************************************/
int GetStr(char string[], int maxLen)
{
int i;
for(i=0;i<maxLen;i++)
{
string[i]=getchar();
if((string[i]=='\n') || (string[i]==EOF))
break;
}
string[i]='\0';
return(i);
}