【问题标题】:Merging 2 text files into a 3rd text file in C++在 C++ 中将 2 个文本文件合并到第 3 个文本文件中
【发布时间】:2014-09-22 05:51:34
【问题描述】:

我正在开发一个程序,该程序应该从 2 个不同的文本文件中获取一定范围的行并将它们合并到第三个文本文件中。我确信这非常简单,但我对 C++ 真的很陌生,不知道应该怎么做。我查看了几个示例,但它们都处理需要排序的输出文件,但我的不需要排序或类似的东西。

这两个文件如下所示:

prog2a.dat:

This is the first line
and this is the second line
and this is the third line
and the fourth
and the fifth
and here is the sixth and
this is the seventh with
the eighth following and the
ninth line here.
The tenth starts a new paragraph
with the eleventh and
the twelfth followed by the
thirteenth line, luck thirteenth
as well as the fourteenth
and the fifteenth and of course
the sixteenth and the
seventeenth.  Are there
any more, yes the eighteenth
and the nineteenth and finally the
twentieth.

prog2b.dat

This is the 1st line of many.
Here is the 2nd of several,
and the 3rd followed by
the 4th as well as the
5th which is the last in this paragraph
A new paragraph has the 6th, followed
by the 7th and
the 8th
and 9th closing with
the 10th.
For the next info the 11th will
lead followed by the 12th and
the 13th with the
14th not far behind and ending
with the 15th.
Finally the 16th
17th and
18th with the
19th and the
20th rounding out the document.

该程序应该从 prog2a 中获取第 5-15 行,从 prog2b 中获取第 4-12 行,并将它们合并到 outfile.dat 中。所以输出应该是这样的:

and the fifth
and here is the sixth and
this is the seventh with
the eighth following and the
ninth line here.
The tenth starts a new paragraph
with the eleventh and
the twelfth followed by the
thirteenth line, luck thirteenth
as well as the fourteenth
and the fifteenth and of course
the 4th as well as the
5th which is the last in this paragraph
A new paragraph has the 6th, followed
by the 7th and
the 8th
and 9th closing with
the 10th.
For the next info the 11th will
lead followed by the 12th and

这是我最近的代码:

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main() {
// Create output file
std::ofstream outFile("outfile.dat", ios::out);

// Open input file 1, if can't be opened, exit
std::ifstream in1("../prog2a.dat");
in1.open();
std::string line;
int count = 1;
    if(!in1) {
        cerr << "Open Failure" << endl;
        exit(1);
    } //end if
    else {
        while(std::getline(in1, line)) {
            if(count >= 5 && count <= 15) {
                outFile << line << "\n";
                ++count;
            }
        } //end while
    } //end else 
in1.close();

//Open input file 2, if can't be opened, exit
ifstream in2;
in2.open("../prog2b.dat");
    if(!in2) {
        cerr << "Open Failure" << endl;
        exit(1);
    } //end if
    else {
        while(std::getline(in2, line)) {
            if(count >= 4 && count <= 12) {
                outFile << line << "\n";
                ++count;
            }
        } //end while
    } //end else
in2.close();
} //end main

在编写代码时,当我尝试使用命令 ./prog2 运行程序时出现以下错误:

./prog2: line 1: /bin: is a directory
./prog2: line 2: Purpose:: command not found
./prog2: line 3: I/O:: No such file or directory
./prog2: line 4: Input:: command not found
./prog2: line 6: prog2a.dat:: command not found
./prog2: line 7: This: command not found
./prog2: line 8: and: command not found
./prog2: line 9: and: command not found
./prog2: line 10: and: command not found
./prog2: line 11: and: command not found
./prog2: line 12: and: command not found
./prog2: line 13: this: command not found
./prog2: line 14: the: command not found
./prog2: line 15: ninth: command not found
./prog2: line 16: The: command not found
./prog2: line 17: with: command not found
./prog2: line 18: the: command not found
./prog2: line 19: thirteenth: command not found
Assembler messages:
Error: can't open well for reading: No such file or directory
well:0: Error: can't open as for reading: No such file or directory
as:0: Error: can't open the for reading: No such file or directory
the:0: Error: can't open fourteenth for reading: No such file or directory
./prog2: line 21: and: command not found
./prog2: line 22: the: command not found
./prog2: line 23: seventeenth.: command not found
./prog2: line 24: any: command not found
./prog2: line 25: and: command not found
./prog2: line 26: twentieth.: command not found
./prog2: line 28: prog2b.dat:: command not found
./prog2: line 29: This: command not found
./prog2: line 30: Here: command not found
./prog2: line 31: and: command not found
./prog2: line 32: the: command not found
./prog2: line 33: 5th: command not found
./prog2: line 34: A: command not found
./prog2: line 35: by: command not found
./prog2: line 36: the: command not found
./prog2: line 37: and: command not found
./prog2: line 38: the: command not found
./prog2: line 39: For: command not found
./prog2: line 40: lead: command not found
./prog2: line 41: the: command not found
./prog2: line 42: 14th: command not found
./prog2: line 43: with: command not found
./prog2: line 44: Finally: command not found
./prog2: line 45: 17th: command not found
./prog2: line 46: 18th: command not found
./prog2: line 47: 19th: command not found
./prog2: line 48: 20th: command not found
./prog2: line 50: Output:: command not found
./prog2: line 52: backup/: is a directory
./prog2: line 57: using: command not found
./prog2: line 59: syntax error near unexpected token `('
./prog2: line 59: `int main() {'

我不知道发生了什么。我对 Unix 还是很陌生,所以这对我来说就像一堆乱码。不确定它是否重要,但我正在使用 g++ (GCC) 4.1.2 20080704 编译器在 Linux 环境中编写此程序。

就像我说的,我对此很陌生,因此非常感谢任何帮助。

【问题讨论】:

  • 先写一个程序,打开一个文件,写入输出文件。然后将其修改为仅写入一系列行。然后添加第二个文件。
  • 这就是我试图做到这一点的方式,将其分解成更小的步骤,但是,我不知道如何让它只写入指定的范围。不过我会着手解决这个问题,当我取得一些进展时会更新我的代码。
  • 您需要创建一个循环来一次读取一行(std::getline())。然后创建一个变量来记录行号。然后添加一个if() 来检查行号是否在要求的范围之间……等等……
  • 类似 while(!in1.eof()) { count++; if(count >= 5 && count
  • 类似的东西是的。但永远不要测试eof()。请改用:while(std::getline(in1, line)) {...},因为只有在读取成功时才会循环。 eof() 不会做你所期望的。

标签: c++ file merge


【解决方案1】:

以下是一些帮助您入门的示例:

复制所有内容:

std::ifstream in1("../prog2a.dat");

std::string line;

while(std::getline(in1, line)) // only loop when read succeeds
{
    // use line here
    outFile << line << '\n'; // copy line to output
}

跟踪行号:

std::ifstream in1("../prog2a.dat");

int count = 1; // line number count (starts at 1)
std::string line;

while(std::getline(in1, line)) // only loop when read succeeds
{
    // use line here
    outFile << line << '\n'; // copy line to output
    ++count; // keep track
}

那么你只需要弄清楚当行号 (count) 是你想要的时如何输出。

提示:永远不要在while() 循环中检查eof()。原因如下:http://www.parashift.com/c++-faq/istream-and-eof.html

【讨论】:

  • 我正在努力在我的代码中实现这一点,但是当我尝试编译“prog2.cpp:65: error: no matching function for call to ?std::basic_ifstream”时出现以下错误 >::open()? /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/ 4.1.2/fstream:495: 注意: 候选者是: void std::basic_ifstream<_chart _traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits]" 不知道这很重要,但我在 Linux 系统上编码。
  • @Beth 您使用的是相当旧的 C++ 编译器版本吗?这有关系吗? stackoverflow.com/questions/6323619/…
  • 我正在使用一个远程登录 Linux 程序,它允许我们使用我们学校的服务器,它使用的是编译器 C++ 4.1.2 20080704(当我输入 g++ --version on无论如何命令行)。我读到您可以使用 -std=c++0x 编译器标志,但不确定在哪里执行此操作,例如在我的代码或命令行中执行,或者它是否可以解决我的问题?
  • 我编辑了原始问题以反映我最近的代码和我遇到的编译器错误。
  • You're line in1.open(); 没有正确调用 open() 并且是不必要的,因为前一行打开了文件(请参阅如何打开 in2 以了解正确的方法)。还需要在读取第二个文件之前将count 重置为1
猜你喜欢
  • 2012-11-30
  • 2020-02-08
  • 2016-10-13
  • 2015-01-20
  • 2019-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多