We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,166 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Writing from one file to another

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;
}
2
Contributors
5
Replies
20 Hours
Discussion Span
6 Months Ago
Last Updated
6
Views
Question
Answered
Daniel BE
Newbie Poster
13 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

deceptikon
Challenge Accepted
Administrator
3,452 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57

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

Daniel BE
Newbie Poster
13 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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;
}
deceptikon
Challenge Accepted
Administrator
3,452 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57

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?....

Daniel BE
Newbie Poster
13 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 6 Months Ago by deceptikon

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.

deceptikon
Challenge Accepted
Administrator
3,452 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0771 seconds using 2.71MB