scanf till EOF

hi, i know how to scanf till EOF but when i need to scan only 1 integer for example
while (scanf("%d",&a)!=EOF)
but when its more than one integer i dont know how, anyone help me?
and, how should i test if my scanf till eof is correct
(in other words, how to represent EOF what sould i input to know if its correct or no)?
Last edited on
Wat r u trying to do? read input from console or read values from a file?
from a console.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<stdio.h>
#include<conio.h>
void main()
{
   int N_values;//stores the no of values
   int Values[100];//assume some fixed big size

while(1)
{
printf("Enter the no of input integers(Max input size 100)\n");
scanf("%d",&N_values);
//check whether input size greater than 100
if(N_values > 100)
printf("Invalid input size\nRe-enter\n");// prompt again if it exceeds
else
break;
}
printf("Enter the integers one by one\n");
for(int i=0;i<N_values;i++)
scanf("%d",&Values[i]);
printf("Here are the integers\n");
for(int i=0;i<N_values;i++)
printf("%d\n",Values[i]);
getch();
}
I would rather recommend using is.good(), where is is the input streaming file.

~ Raul ~
scanf(3) returns (see its man page):

1. > 0: The number of items scanned successfully.
2. = 0: There is more input but the next character to be scanned doesn't match syntactically the first input type.
3. EOF: Any error or end-of-file occurred.

So to get all input variables being well initialized you may better run a positive test of scanf(3) return value.

If you want to get input into more than one variable from only one scanf(3) expression, a positive test may be inevitable to avoid running out of sync with your input data stream.

@jumper007: Using is.good() does only work well having initially synchronized both stdin method (C-stdio and C++-streaming) because they use different buffers for the same stream. See http://www.cplusplus.com/reference/ios/ios_base/sync_with_stdio/. But mixing both methods usually makes sense only if you're using libraries which use another method than your preferred one.

@anirudh sn: Posix stdio doesn't distinguish between console input or those from other files. There's also no <conio.h> available nor needed when using Posix stdio. May be you need it to get those non-Posix getch() function prototype? I've never seen it before.
yes.
A better way is to use fgets and sscanf to read information from the array filled by fgets

1
2
3
4
5
char BUFF[1024];
while(fgets(BUFF, 1024, stdin))
  {
    sscanf(BUFF, "%(const)", ...);
  }


1
2
3
4
while(fscanf(stdin, "%d", &a))
  {
    //do something here
  }


fgets in c is the equivalent of getline in c++

Note: if you are reading from a file, stdin, should be replaced by the varaible name representing the file. otherwise stdin will read from standard input. I prefer the first one because it has a nicer level of control
Last edited on
@Smac89: Your first solution may fail if the input line is longer than sizeof(BUFF) - 1. Then an input token may be split over two buffers. You'll also double buffer your input: The first time in stdin and then in BUFF.

a nicer level of control
if you need look ahead you may want to use ungetc(3) or write your own tokenizer with look ahead of an entire token value and its type.
Topic archived. No new replies allowed.