0

Anyone knows how to open a text file and write the content of it to a text file with command line or arguments, i mean using the argc and argv.

thanx for answer!

3
Contributors
2
Replies
4
Views
8 Years
Discussion Span
Last Post by Ancient Dragon
0
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(int argc,char* argv[])
string input;
if(argc<2){
cout<<"Enter a command line argument!!"<<endl;
return 1;
}
ofstream fout("new.txt");
cout<<"Enter some text to the file..";
cin>>input;
fout<<input;
fout.close;
ifstream fin("new.txt");
if(!fin.is_open()){
cout<<"Unable to open file!!"<<endl;
return 1;
}
return 0;
}

The executable of this code can only work if some command-line arguments are added.
Hope this helped!!(Pls, you would show your appreciation by adding to my reputation)

0
int main(int argc, char*argv[])
{
   ofstream out;
   if( argc == 2)
   {
      out.open(argv[1]);
       if( !out.is_open())
       {
           cout << "Failed to open the file \"" << argv[1] << "\"\n";
           return 1;
       }
   {
   else
   {
        cout << "usage: myprog filename\n";
        return 1`;
   }
   // now write to the file
  ...
}

Edited by Ancient Dragon: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.