使用命令行参数合并2个文件的C ++程序。 [英] C++ program to merge 2 files using command line arguments.

查看:69
本文介绍了使用命令行参数合并2个文件的C ++程序。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以发布此问题的程序

can any one post the program for this question

推荐答案

merge file1 file2 file3



合并程序应该从命令行获取参数(参见 argc argv ,它然后应该读取每个文件并合并数据(以您需要的任何方式)并将输出写入file3。请参阅的手册页fopen() fread() fwrite()了解更多信息。


The merge program should take the parameters from the command line (see argc and argv, it should then read each file and merge the data (in whatever way you require) and write the output to file3. See the man pages for fopen(), fread(), and fwrite() for more information.


以下程序接受4命令行参数。第一个参数(argv [0])被假定为程序名。第二个和第三个参数是要合并的文件名。最后一个参数是输出文件,合并的内容保存到该文件中。



The following program accept 4 command line arguments. First argument ( argv[0] ) is assumed as program name. second and third arguments are file names to be merged. Last argument is the output file, to which the merged contents saved.

#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
#include <process.h>
int main(int argc, char* argv[] )
{

    char ch;
    ifstream infile;
    infile.open( argv[1] );
    if( !infile )
    {
        cerr << "\nCan't open " << argv[1];
        exit(-1);
    }
    ofstream outfile;
    outfile.open( argv[3] );
    if( !outfile )
    {
        cerr << "\nCan't open " << argv[3];
        exit(-1);
    }
    while( infile )
    {
        infile.get(ch);
        outfile.put(ch);
    }
    infile.close();

    infile.open( argv[2] );
    if( !infile )
    {
        cerr << "\nCan't open " << argv[2];
        exit(-1);
    }

    while( infile )
    {
        infile.get(ch);
        outfile.put(ch);
    }
    infile.close();
    outfile.close();
    return 0;
}





希望这对你有用



Hope that this is useful for you


根据你的问题这将对你有所帮助



C:\>输入text1.txt

这是一个测试。

然后你可以使用type命令加上双箭头来合并文件



C:\> type * .txt>> merge.txt

text1.text

text2.text等...



并注意如果你使用linux就可以使用:



cat * .txt>> merge.txt(>> ==表示输入流)
according to your question this will help you

C:\>type text1.txt
this is a test.
then you can use the type command plus the double arrows to merge a files

C:\>type *.txt >>merge.txt
text1.text
text2.text etc...

and note if you using linux you can use :

cat *.txt >> merge.txt (>> ==means input streams)


这篇关于使用命令行参数合并2个文件的C ++程序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆