What happens is that you're not checking for end of file, and 1 and 2
are still stored in memory so you'll output them again. When you're
inputting the values manually, you don't notice this since the program
pauses to wait for further input.

This should work. It verifies that fscanf did indeed manage to read
two variables:

#include <cstdio>

int main()
{
  int a,b;
  while(2 == fscanf(stdin, "%d%d", &a, &b))
    fprintf(stdout, "%d %d ", a, b);

  return 0;
}


On May 6, 11:17 pm, junaid ahmed <[email protected]> wrote:
> when i used fscanf and fprintf then every thing is working fine but the
> output file is filled with the output many times.
>
> ex:
> if the out put should be *1 2 *
> then the output is like *1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2
> *.........................
> infinit times
>
> please help me
>
> On Sat, May 7, 2011 at 1:38 AM, hemant kumar- I NEVER BACK DOWN <
>
>
>
> [email protected]> wrote:
> > write scanf("%d %d",&a,&b);
>
> > On Sat, May 7, 2011 at 1:35 AM, hemant kumar- I NEVER BACK DOWN <
> > [email protected]> wrote:
>
> >> hey, don't use that getch(); , it will make your output wrong , as it may
> >> introduce some invalid ascii character
>
> >> On Sat, May 7, 2011 at 1:27 AM, junaid ahmed 
> >> <[email protected]>wrote:
>
> >>> dude i even tried doing so, but i dont knw why its not working. i m using
> >>> windows OS . I am really worried. plz help me out
>
> >>> regards
>
> >>> On Sat, May 7, 2011 at 1:18 AM, hemant kumar- I NEVER BACK DOWN <
> >>> [email protected]> wrote:
>
> >>>> u did wrong in line  freopen("B-small-practice","r",stdin);   u missed
> >>>> to written .in at last ,
>
> >>>> it will be  freopen("B-small-practice.in","r",stdin)
>
> >>>> On Sat, May 7, 2011 at 12:24 AM, junaid ahmed <
> >>>> [email protected]> wrote:
>
> >>>>> i used ur concept of taking input from a file.. but i m not able to get
> >>>>> the correct answer in the output file.
>
> >>>>> my code:
> >>>>> #include<stdio.h>
> >>>>> #include<conio.h>
> >>>>> void main()
> >>>>> {
> >>>>> int a,b,c;
> >>>>> freopen("B-small-practice","r",stdin);
> >>>>>  freopen("B.out","w",stdout);
> >>>>> scanf("%d",&a);
> >>>>>  scanf("%d",&b);
> >>>>> c=a+b;
>
> >>>>> printf("Out put = %d",c);
> >>>>>  getch();
> >>>>> }
>
> >>>>> please help me out;
> >>>>> thanks
>
> >>>>> On Thu, May 5, 2011 at 11:47 PM, hemant kumar- I NEVER BACK DOWN <
> >>>>> [email protected]> wrote:
>
> >>>>>> i downloaded the solved code for problem reverse string from
> >>>>>> scoreboards of winner  that was
>
> >>>>>> #include <stdio.h>
>
> >>>>>> #include <string.h>
>
> >>>>>> int main()
>
> >>>>>> {
>
> >>>>>> int t,j,k,i,n;
>
> >>>>>> char x[1010];
>
> >>>>>>  freopen("in","r",stdin);
>
> >>>>>> freopen("B.out","w",stdout);
>
> >>>>>>  scanf("%d",&t);
>
> >>>>>>  for(k=0;k<t;++k) {
>
> >>>>>> scanf("\n%[^\n]",x);
>
> >>>>>>  n = strlen(x);
>
> >>>>>>  printf("Case #%d:",k+1);
>
> >>>>>>  while(n>=0) {
>
> >>>>>> for(i=n-1;i>=0;--i)
>
> >>>>>> if(x[i] == ' ') break;
>
> >>>>>>  printf(" ");
>
> >>>>>> for(j=i+1;j<n;++j) printf("%c",x[j]);
>
> >>>>>>  n = i;
>
> >>>>>> }
>
> >>>>>>  printf("\n");
>
> >>>>>> }
>
> >>>>>> }
>
> >>>>>> but, when i m submitting that  ,,  i get error your output should
> >>>>>> start with case #1:  , even though it's correct. plz help. the code 
> >>>>>> posted
> >>>>>> is of the winners of code jam 2010 . there code is showing this error 
> >>>>>> on my
> >>>>>> macbook, safari browser. what's da reason..
>
> >>>>>> On Thu, May 5, 2011 at 11:09 PM, Rahul Gulati <
> >>>>>> [email protected]> wrote:
>
> >>>>>>> Use wordpad on windows to open the file and copy the contents to a
> >>>>>>> new text file.
>
> >>>>>>> On Thu, May 5, 2011 at 11:01 PM, chase 
> >>>>>>> <[email protected]>wrote:
>
> >>>>>>>> when i m downloading the input text file on my macbook , it is
> >>>>>>>> displaying as :::
>
> >>>>>>>> 10
> >>>>>>>> 100
> >>>>>>>> 2
> >>>>>>>> 445 5476 67
> >>>>>>>> 545 545
> >>>>>>>> 3
> >>>>>>>> 44 45545
>
> >>>>>>>> but when i m downloading in my window laptop , the downloaded input
> >>>>>>>> file is showing as   :::
>
> >>>>>>>> 10 100 2 445 5476 67 545 545 3 44 45545
>
> >>>>>>>> plz tell  which input file i will prefer for programming  ,,,   in
> >>>>>>>> windows there's no newline \n , but in macbook it is \n  newline .
> >>>>>>>> plz
> >>>>>>>> suggest . fast
>
> >>>>>>>> --
> >>>>>>>> You received this message because you are subscribed to the Google
> >>>>>>>> Groups "google-codejam" group.
> >>>>>>>> To post to this group, send email to [email protected].
> >>>>>>>> To unsubscribe from this group, send email to
> >>>>>>>> [email protected].
> >>>>>>>> For more options, visit this group at
> >>>>>>>>http://groups.google.com/group/google-code?hl=en.
>
> >>>>>>> --
> >>>>>>> Rahul Gulati
> >>>>>>> [email protected]
> >>>>>>> Department of Computer Science
> >>>>>>> Jaypee Institute of Information Technology (Deemed University)
> >>>>>>> India
>
> >>>>>>>  --
> >>>>>>> You received this message because you are subscribed to the Google
> >>>>>>> Groups "google-codejam" group.
> >>>>>>> To post to this group, send email to [email protected].
> >>>>>>> To unsubscribe from this group, send email to
> >>>>>>> [email protected].
> >>>>>>> For more options, visit this group at
> >>>>>>>http://groups.google.com/group/google-code?hl=en.
>
> >>>>>>  --
> >>>>>> You received this message because you are subscribed to the Google
> >>>>>> Groups "google-codejam" group.
> >>>>>> To post to this group, send email to [email protected].
> >>>>>> To unsubscribe from this group, send email to
> >>>>>> [email protected].
> >>>>>> For more options, visit this group at
> >>>>>>http://groups.google.com/group/google-code?hl=en.
>
> >>>>>  --
> >>>>> You received this message because you are subscribed to the Google
> >>>>> Groups "google-codejam" group.
> >>>>> To post to this group, send email to [email protected].
> >>>>> To unsubscribe from this group, send email to
> >>>>> [email protected].
> >>>>> For more options, visit this group at
> >>>>>http://groups.google.com/group/google-code?hl=en.
>
> >>>>  --
> >>>> You received this message because you are subscribed to the Google
> >>>> Groups "google-codejam" group.
> >>>> To post to this group, send email to [email protected].
> >>>> To unsubscribe from this group, send email to
> >>>> [email protected].
> >>>> For more options, visit this group at
> >>>>http://groups.google.com/group/google-code?hl=en.
>
> >>>  --
> >>> You received this message because you are subscribed to the Google Groups
> >>> "google-codejam" group.
> >>> To post to this group, send email to [email protected].
> >>> To unsubscribe from this group, send email to
> >>> [email protected].
> >>> For more options, visit this group at
> >>>http://groups.google.com/group/google-code?hl=en.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "google-codejam" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group at
> >http://groups.google.com/group/google-code?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"google-codejam" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-code?hl=en.

Reply via email to