24小时热门版块排行榜    

查看: 604  |  回复: 2
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

小木的微笑

木虫 (初入文坛)

[求助] C程序编程题 已有1人参与

应用C程序实现将若干个文件的内容依次复制插入到一个新文件new.txt中。

[ 发自手机版 http://muchong.com/3g ]
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

小木的微笑

木虫 (初入文坛)

送红花一朵
引用回帖:
2楼: Originally posted by 木叶清风 at 2014-01-02 16:00:12
int main(int argc, char* argv [])
{
        if (argc < 3)
        {
                printf("You should input like this: exename outputfilename inputfilename_1 inputfilename_1 ... inputfilename_n\n";
                exit(-1 ...

非常感谢帮助!

[ 发自小木虫客户端 ]
3楼2014-01-03 18:18:33
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 3 个回答

木叶清风

木虫 (正式写手)

【答案】应助回帖

★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★
感谢参与,应助指数 +1
小木的微笑: 金币+50, ★★★★★最佳答案 2014-01-03 18:19:02
xzhdty: 谢谢参与 2014-01-04 21:40:23
int main(int argc, char* argv [])
{
        if (argc < 3)
        {
                printf("You should input like this: exename outputfilename inputfilename_1 inputfilename_1 ... inputfilename_n\n";
                exit(-1);
        }

        FILE *fout = fopen(argv[1], "w";
        if (fout == NULL)
        {
                printf("Cannot open file %s to write results.\n", argv[1]);
                fclose(fout);
                exit(-1);
        }

        FILE *fin;
        for (int i = 2; i < argc; ++i)
        {
                fin = fopen(argv, "r";
                if (fin == NULL)
                {
                        printf("Cannot read input file: %s\n", argv);
                        fclose(fout);
                        fclose(fin);
                        exit(-1);
                }

                char c;
                //        size_t num = 0;
                while ((c = getc(fin)) != EOF) {
                        putc(c, fout);
                }
                putc('\n', fout);
                fclose(fin);
        }

        fclose(fout);
        return 0;
}

有的人可能会用下面的方法来读取整个文件,但是这样的方法处理"\r\n"的时候处理不好,得到的文件大小很多时候会出现错误。
char *fcontent = NULL;
        int fsize = 0;
       
        fseek(fp, 0, SEEK_END);
        fsize = ftell(fp);
        fseek(fp, 0, SEEK_SET);

        fcontent = (char*) malloc(sizeof(char) * fsize);
        fread(fcontent, 1, fsize, fp);
       
        return fcontent;

当然了,用c++还是比较方便的
        ofstream ofs(argv[1], ofstream:ut | ofstream::binary);
        ostream_iterator<char> ostm(ofs, "\n";
        for (int i = 2; i < argc; ++i)
        {
                ifstream ifs(argv, ifstream::in |ifstream::binary);
                ifs >> std::noskipws;
                copy(istream_iterator<char>(ifs), istream_iterator<char>(), ostm);
                ostm++ = '\r';
                ostm++ = '\n';
        }
上面的代码基本就可以了,单行空格也是可以处理的。

» 本帖已获得的红花(最新10朵)

www.cvdelver.com
2楼2014-01-02 16:00:12
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
信息提示
请填处理意见