Write a function We want a function, called split, that has three parameters: on
ID: 3934907 • Letter: W
Question
Write a function
We want a function, called split, that has three parameters: one input file stream, and two output file streams. The function split does not open the stream itself. It reads the first number from the input file, let's call this number f, and uses f to split the input file into one file with all the numbers that are less or equal to f and one file with all the numbers that are greater than f. The number less or equal to f and one file with all the numbers that are greater than f. The number less or equal to f are written into the file connected to the first output file stream parameter and those greater are written into the file connected to the second output file stream parameter.
Hint; It is possible that the input file is empty.
Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
void split(ifstream& in,ofstream& o1,ofstream& o2)
{
int num;
int f;
in>>f; //Take first number
while(in>>num)
{
if(num>f) //if number is less than first number then write to Output 1
{
o1<<num<<endl;
}
else
{
o2<<num<<endl; //if number is greater than first number then write to Output 1
}
}
o2.close();
o1.close();
}
int main(int argc, char const *argv[])
{
ifstream in("input.txt");
ofstream o1("output1.txt",ios::out);
ofstream o2("output2.txt",ios::out);
split(in,o1,o2);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.