Adding parameters to the File replicator on the command line

Source: Internet
Author: User

This blog post describes the programming method for using command-line arguments, which are used for file replication. This is also the first blog post I wrote with the Markdown editor. Markdown used it when he wrote "The University of the reverse" and wrote the blog for the first time.

The text of this article:

Introduction

The following program can complete the replication from A.txt to B.txt.

#include <iostream>#include <fstream>usingnamespacestd;int main( ){    char ch;    ifstream infile("a.txt",ios::in);    ofstream outfile("b.txt.dat");    while(infile.get(ch))        outfile.put(ch);    infile.close( );    outfile.close();}

The drawback of this procedure is, to copy other files, can only tell the user: "You change the program to go." "Day, Xiao Cheng classmate, you really want to lose your job."
You can take the practice of letting the user enter the file name in the program run. If running, prompt: "Please enter source file:" Input a.txt or other, then prompt: "Please enter the target file:", enter B.txt or other, a can meet the needs of the "file Replicator" completed.

Using the command line

Here's a list of parameters that are widely used in the command-line world, and this is what you'll want to introduce to your disciples.
Our goal is to:

Run: d:\cb\cpy>cpy source.c temp.c
Result: Copy source.c, generate new file temp.c

Practice

Build Project CPY (You can also create additional project names, and finally generate the corresponding. exe executable file)
The program uses the main function with the parameters: int main (int argc, char *argv[]);

which

    • int argc; The command line is composed of several parts
    • Char *argv[]; Array of pointers, strings pointing to parts

such as running

cpy source.c temp.c

The parameters argc and argv, respectively, are as follows:

In this way, the following programs can be programmed:

#include <iostream>#include <fstream>#include <cstdlib>using namespace STD;intMainintargcChar*argv[]) {CharChif(ARGC <3)    {Cerr<<"You must provide the source and target file.\n";Exit(1); } ifstream infile (argv[1],ios::in); Ofstream outfile (argv[2], ios::out); while(Infile.get (CH)) outfile.put (CH);    Infile.close (); Outfile.close ();}

After compiling, connecting, find the resulting. exe file, you can use the form of the command line to complete the copy of the file.

Expand

Ask a question, if you use

cpy a.txt b.txt .... x.txt

form, how do you want to merge the contents of multiple files into x.txt?
Finding the number of files to merge here is uncertain, which is the most critical problem to solve.
In fact, as long as the command line is entered, the number of files is determined.
The command line altogether gave the ARGC parameter, the source file altogether has argc-2, respectively is argv[1] to argv[argc-2], the target file is argv[argc-1], constructs a cycle to complete this project task.
The procedure obtained is:

#include <iostream>#include <fstream>#include <cstdlib>using namespace STD;intMainintargcChar*argv[]) {CharChif(ARGC <3)    {Cerr<<"provide at least 2 filenames. \ n";Exit(1); } ofstream outfile (argv[argc-1], ios::out); Ifstream infile;intI for(i=1; i<=argc-2; ++i) {infile.open (argv[i],ios::in);//Open source file         while(Infile.get (CH))//Complete replicationOutfile.put (CH); Infile.close ();//Close the file, loop back and then open another} outfile.close ();return 0;}

Adding parameters to the File replicator on the command line

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.