Monday, January 20, 2014

C++ Program to Create Odd and Even Files Using Command Line Arguments

with 0 Comment
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<fstream.h>
void main(int argc,char *argv[])
{
int x[10],n,i;
char c;
cout<<"\n Enter the no.of array size:";
cin>>n;
cout<<\n Enter the nos one by one";
for(i=0;i<n;i++)
{
cin>>x[i];
}
if(argc!=3)
{
cout<<"\n Insufficient no of arguments";
exit(0);
}
ofstream f1,f2;
f1.open(argv[1]);
if(f1.fail())
{
cout<<"\n The File is already exists";
exit(0);
}
f2.open(argv[2]);
if(f2.fail())
{
cout<<"\n The File is already exists";
exit(0);
}
for(i=0;i<n;i++)
{
if(x[i]%2==0)
f2<<x[i];
else
f1<<x[i];
}
f1.close();
f2.close();
ifstream fn1,fn2;
fn1.open(argv[1]);
cout<<"\n Content of the odd file\n";
while(!fn1.eof())
{
fn1.get(c);
cout<<c;
}
fn1.close();
fn2.open(argv[2]);
cout<<"\n Content of the even file\n";
while(!fn2.eof())
{
fn2.get(c);
cout<<c;
}
fn2.close();
getch();
}


------------
OUTPUT:
------------

D:\TC>COMMAND ODD EVEN

Enter the no.of array size:5

Enter the nos one by one:
9
8
7
6
5
Contentof the odd file
9    7    5
Content of the even file
8    6

D:\TC>TYPE ODD
975
D:\TC>TYPE EVEN
86

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive