Write a program to create a numeric file and split the file into two files, one containing all odd
numbers and another with even numbers.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void WriteFile()
{
string data, numbers = "0123456789";
ofstream dataFile("data.txt");
cout << "Enter all the numeric data: ";
getline(cin, data);
for (int i = 0; i < data.length(); ++i)
{
if (numbers.find(data[i]) != string::npos)
{
dataFile << data[i];
}
}
}
void SplitFile()
{
string data;
ifstream dataFile("data.txt");
ofstream even("even.txt");
ofstream odd("odd.txt");
dataFile >> data;
for (int i = 0; i < data.length(); ++i)
{
if (data[i] % 2 == 0)
{
even << data[i];
}
else
{
odd << data[i];
}
}
}
int main()
{
WriteFile();
SplitFile();
}
Comments
Leave a comment