1,105,625 Community Members

Writing from one file to another

Member Avatar
Daniel BE
Newbie Poster
13 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Dear friends,
I want to create a program that reads 10 numbers from the user and store it in a file namely Numbers.txt.
After that I have to read each numbers and if it is a Even number then it should be stored in a file "Even.txt" otherwise it should be stored in a file "Odd.txt".
I craeted the program and while executing the files are created correctly but when it is reading and displaying the odd and even numbers from their file the last number is printed twice. I used eof() and good() function but there is no change in the output.
so please tell me why it is printed twice and also how to rectify it ?

The programs is shown below

#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;

int main()
{

     int x,i ,n;
     fstream fout("Numbers.txt",ios::out);
     cout<<"Enter the Numbers: \n";
     for(i=0;i<10;i++)
     {
          cin>>x;
          fout<<x<<"\n";
     }
     fout.close();
     fstream fin("Numbers.txt",ios::in);
     fstream outfile2("Even.txt",ios::out);
     fstream outfile3("Odd.txt",ios::out);
     for(i=0;i<10;i++)
     {
          fin>>n;
          if(n%2==0)
             outfile2<<n<<"\n";
          else
            outfile3<<n<<"\n";
     }
     fin.close();
     outfile2.close();
     outfile3.close();
     fstream infile2("Even.txt",ios::in);
     cout<<"\nEven File Contains:";
     while(infile2.good())
     {
      infile2>>n;
      cout<<"\n"<<n;
     }
     fstream infile3("Odd.txt",ios::in);
     cout<<"\nOdd File Contains:";
     while(infile3.good())
     {
       infile3>>n;
       cout<<"\n"<<n;
     }
     return 0;
}
Member Avatar
deceptikon
Eternally Awesome
4,694 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 689 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
1
 

I used eof() and good() function but there is no change in the output.

They're both wrong, so I'm not surprised. The problem is that both eof() and good() will only switch state after you've tried to read from the file and that attempt fails. So you need some way to check the state after reading but before processing what was read, otherwise you'll accidentally process the previously read item again.

Convention is this:

while (infile2 >> n)
    cout << '\n' << n;

The extraction operator will return a reference to the stream object, which when used in boolean context will give you the current state. Alternatively you can still use the eof() or good() option, but a conditional inside the loop is needed:

while (infile2.good()) {
    if (!infile2.good())
        continue;

    cout << '\n' << n;
}

That's redundant, verbose, and logically confusing, so I don't recommend it.

Member Avatar
Daniel BE
Newbie Poster
13 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you deceptikon Amazing, it works correctly,but the second method is not working it is printing the last entered number infinitely..

Member Avatar
deceptikon
Eternally Awesome
4,694 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 689 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
1
 

but the second method is not working it is printing the last entered number infinitely..

My bad, I forgot to include the actual input request:

while (infile2.good()) {
    infile2 >> n;

    if (!infile2.good())
        continue;

    cout << '\n' << n;
}
Member Avatar
Daniel BE
Newbie Poster
13 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

My bad, I forgot to include the actual input request:

Thanks again, it works........I think In if statement we are checking the next position is good or not?
Am I Right?....

Question Answered as of 1 Year Ago by deceptikon
Member Avatar
deceptikon
Eternally Awesome
4,694 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 689 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

Thanks again, it works........

Please don't use that longer loop for anything but your own instruction. The conventional method is strongly recommended.

I think In if statement we are checking the next position is good or not?

No, we're checking if the previous input request tried to read of the end of the stream or encountered an error.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: