Learning to read from an input file 
Author Message
 Learning to read from an input file

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?
2.  Does my test character need to be of type int or type char?
3.  How do I capture the EOF and get out of the loop?

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
        int i=0;

        while(c != EOF)
        {
                scanf("%s", c);
                printf("The input is %s", c);
        }
return(0);

Quote:
}

--



Mon, 22 Mar 2004 02:40:53 GMT  
 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
--



Tue, 23 Mar 2004 03:21:43 GMT  
 Learning to read from an input file


Quote:
> Questions:
> 1.  Is EOF an integer?
> 2.  Does my test character need to be of type int or type char?
> 3.  How do I capture the EOF and get out of the loop?

See section 12 at http://www.faqs.org/faqs/C-faq/faq/.

--
--------------------------------
A. Sinan Unur
http://www.unur.com/
--



Tue, 23 Mar 2004 03:21:50 GMT  
 Learning to read from an input file

Quote:

> 1.  Is EOF an integer?

Yes, and its value is negative.

Quote:
> 2.  Does my test character need to be of type int or type char?

Depends on whether it can be EOF.  See below.

Quote:
> 3.  How do I capture the EOF and get out of the loop?

If you want to process one character at a time, you should use
getc or getchar, not scanf.  Store the result in an int variable
and check if it's EOF.

  int i;
  while ((i = getchar()) != EOF)
    printf("The input is %c", i);

The reason why the variable must be int, not char, is that
getchar returns either EOF or an unsigned char converted to int.
On typical implementations of C, both char and unsigned char have
256 possible values; even if the compiler treats plain char as
unsigned so that all the values fit, there is no value left for
EOF.

With functions like fgets or scanf, you must have an array of
char rather than an array of int, because the functions report
end-of-file in a different way and don't store special values in
the array.
--



Tue, 23 Mar 2004 03:21:55 GMT  
 Learning to read from an input file
everyone has probably beaten me to it but, you need an int variable to
test for equality with EOF.
--



Tue, 23 Mar 2004 03:22:13 GMT  
 Learning to read from an input file

Quote:

> 1.  Is EOF an integer?

Yes. And it's a negative one, too.

Quote:
> 2.  Does my test character need to be of type int or type char?

If it really is a single character, and you want to check for EOF:
int.

Quote:
> 3.  How do I capture the EOF and get out of the loop?

In your particular case, using scanf("%s"): by looking at the return
value of your scanf() call.  Or at the return value of feof(stdin), if
you wish.
--

Even if all the snow were burnt, ashes would remain.
--



Tue, 23 Mar 2004 03:22:15 GMT  
 Learning to read from an input file
Thank you all for the help.

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?
> 2.  Does my test character need to be of type int or type char?
> 3.  How do I capture the EOF and get out of the loop?

> 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
>         int i=0;

>         while(c != EOF)
>         {
>                 scanf("%s", c);
>                 printf("The input is %s", c);
>         }
> return(0);
> }
> --


--



Tue, 23 Mar 2004 12:54:02 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Learning to read an input file

2. Help: Reading Input Files using fget & strtok

3. how to read input from files

4. Reading 2 lines from input file

5. HELP reading file input into variables

6. reading input from a file, help!!!

7. reading input file problem

8. use of istream to read records from input file

9. reading 1 dimensional data from an input file??

10. reading from stdin ending the read without limitation on the input

11. Need input about learning C/C++

12. People who want to learn C/C++, read this

 

 
Powered by phpBB® Forum Software