0

I have written the following code. What I did is this

  • opened a file "Numbers.dat" and asked input from console
  • then separated the numbers in separate even and odd files
#include <fstream>
#include <iostream>

int main() {
    std::ofstream numWrite("Numbers.dat");
  
    int temp;
    while(std::cin>>temp){
    numWrite<<temp<<std::endl;
    }
    numWrite.close();

    std::ofstream even("Even.dat"), odd("Odd.dat");
    std::ifstream num("Numbers.dat");

    
    while(num){
        num>>temp;
        if(temp%2==0)
            even<<temp<<std::endl;
        else
            odd<<temp<<std::endl;
    }

    num.close();
    even.close();
    odd.close(); 

  return 0;
}

I used newlines after every input, therefore I am having the extra newline in Numbers.dat file and when my program is reading that, it is giving an extra output to the even/odd files.

I can eliminate them by either removing newline from the numbers.dat file, or some check on the other code. But I am unable to do that, please help! Or if there is a better way, tell me that too!

Inputs:

1 2 3 4 5 6 7 8 9 10 a

OUTPUTS:

even.dat

2
4
8
10
10

odd.dat

1
3
5
7
9
  • 3
    This doesn't address the question, but inserting all those `std::endl`s into a file will kill performance. You don't need to flush the file after each insertion; the file knows better than you do when to flush. Just use `'\n'`. – Pete Becker Feb 05 '23 at 18:09
  • 2
    Also, you don't need to call `close()` on those streams; the destructors will do that. – Pete Becker Feb 05 '23 at 18:09
  • 3
    The problem appears to be `while(num){num>>temp;...}`. When the input fails the code still writes to the output file, then the loop checks `num` to see if it succeeded. Should be `while (num >> temp)`. – Pete Becker Feb 05 '23 at 18:12
  • I'm sure this is a duplicate. @PeteBecker is correct; the status of `num` isn't updated until *after* you've attempted a read and it failed. – Mark Ransom Feb 05 '23 at 18:33
  • Earlier in the code you have `while(std::cin>>temp)` which checks for failure. Later you have `while(num) { num>>temp;` which doesn't check for failure. The failure happens, and the code doesn't handle it. Sad panda. – Eljay Feb 05 '23 at 19:20
  • Thank you so much for making this clear. I don't know why I hesitated in asking here, but this is good. Thank you again :) – Devashish Negi Feb 05 '23 at 22:13

1 Answers1

0

Problem is this:

    while(num){
        num>>temp;

This should be done this way:

    while(num>>temp){

For details see eof() bad practice?

Fancy way to address this is, by use of iterators and std::partition_copy

#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>

int main() {
    std::ifstream in("Numbers.dat");
    std::ofstream even("Even.dat");
    std::ofstream odd("Odd.dat");
  
    std::partition_copy(
        std::istream_iterator<int>{in}, {},
        std::ostream_iterator<int>{even, " "},
        std::ostream_iterator<int>{odd, " "},
        [](auto x) { return x % 2 == 0; });

    return 0;
}

https://godbolt.org/z/z4qcErPWd

Marek R
  • 32,568
  • 6
  • 55
  • 140