
Learning to read from an input file
Quote:
> Hello All,
> I am trying to learn to read from an input file. My eventual goal is to
> read each character adn then perform an action based on the character. I
> have my code listed below. I have formed an infinte loop and don't know how
> to break out of it. Any help or suggestions is/are greatly appreciated.
> Background: I telnet into a UNIX machine
> I program in ksh and vi is my editor
> I have tried while(c != EOF)
> and while(i != EOF)
> My data is a text file that look like: " This is my
> data it includes tabs"
> I run it by a.out < inputdata
> Questions:
> 1. Is EOF an integer?
Yes. Usually -1, but don't count on it; use EOF
Quote:
> 2. Does my test character need to be of type int or type char?
int
Quote:
> 3. How do I capture the EOF and get out of the loop?
See code below.
Quote:
> Thank you for your help. My code is below.
> #include <stdio.h>
> #define N 8 //N is the fixed UNIX tab length of 8 spaces
> int main()
> {
> char c[2000]; // c is the holder for current character
You don't need 2000 characters to hold one character!
Quote:
> int i=0;
> while(c != EOF)
> {
> scanf("%s", c);
> printf("The input is %s", c);
> }
> return(0);
> }
int main()
{
int c;
while ( (c = getc(stdin)) != EOF )
{
printf("The input is %c - %3d\n",c,c);
}
return 0;
Quote:
}
--
Chris F.A. Johnson http://cfaj.freeshell.org
===================================================================
My code (if any) in this post is copyright 2001, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
--