Cannot write the output to a text file in cpp program

Posted by swapedoc on Stack Overflow See other posts from Stack Overflow or by swapedoc
Published on 2013-06-25T15:13:05Z Indexed on 2013/06/25 16:21 UTC
Read the original article Hit count: 266

I have this input file "https://code.google.com/codejam/contest/351101/dashboard/do/A-large-practice.in?cmd=GetInputFile&problem=374101&input_id=1&filename=A-large-practice.in&redownload_last=1&agent=website&csrfmiddlewaretoken=OWMxNTVmMTUyODBiYjhhN2Q2OTM3ZGJiMTNhNDkwMDF8fDEzNzIxNzI1NTE3ODAzMjA%3D" I tried to read this file :-using freopen("filename.txt",r,stdin); and then I wanted the output written to be written to another text file which I can upload in this codejam practice question for the judge.

#include<iostream>
   #include<cstdio>
   using namespace std;
   int main()
   {
     int t,k=0,a[2000];
     freopen("ab.txt","r",stdin);

     scanf("%d",&t);

     while(t--)
     {
         freopen("cb.txt","w",stdout);

         int c;

         scanf("%d",&c);
         int n;
         scanf("%d",&n);
         for(int i=0;i<n;i++)
         scanf("%d",&a[i]);

         printf("Case #%d: ",++k);
         for(int i=0;i<n-1;i++)
         {for(int j=i+1;j<n;j++)
          if((a[i]+a[j])==c)
           {printf("%d %d\n",i+1,j+1);
             i=n;}
           }

     }
     return 0;
    }

This is my code. Now the problem is the output file cb.txt contains only the last line of the input. I want the the whole of the output to be written to cb.txt,so what should I do.

© Stack Overflow or respective owner

Related posts about c++

Related posts about file-handling