1

I'm working on a program that is supposed to take a certain range of lines from 2 different text files and merge them into a third text file. I'm sure this is incredibly simple, but I'm really new to C++ and can't figure out how this should be done. I've looked over several examples, but they all deal with output files that need to be sorted, but mine doesn't need to be sorted or anything like that.

The two files look like this:

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.

The program is supposed to take lines 5-15 from prog2a and lines 4-12 from prog2b and merge them into outfile.dat. So the output should look something like this:

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

Here is my most recent code:

#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

As the code is written, I'm getting the following error when I try to run the program using the command ./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() {'

I have no idea what's going on. I'm still really new to Unix so this just looks like a bunch of gibberish to me. Not sure if it's important or not, but I am writing this program in a Linux environment using the g++ (GCC) 4.1.2 20080704 compiler.

Like I said, I'm really new to this so any help is greatly appreciated.

  • 1
    First write a program that opens one file and writes it to the output file. Then modify it to write only a range of lines. Then add the second file. – molbdnilo Sep 22 '14 at 05:58
  • That's kind of how I'm trying to do this, breaking it up into smaller steps, however, I have no clue how to make it only write the specified range. I'll get to working on this though and will update my code when I've made some progress. –  Sep 22 '14 at 06:01
  • You need to make a loop to read one line at a time `(std::getline()`). Then create a variable to keep count of the line number. Then add an `if()` to check if the line number is between the required range... etc... – Galik Sep 22 '14 at 06:02
  • So something like while(!in1.eof()) { count++; if(count >= 5 && count <= 15) { //write line to file }} ? I know that's java and I'll have to look up how to accomplish that in C++ if that's the right way to go about it, just wanted to make sure I'm understanding the concept @Galik –  Sep 22 '14 at 06:09
  • Something like that yes. BUT never test for `eof()`. Use this instead: `while(std::getline(in1, line)) {...}` because that will only loop if the read succeeds. `eof()` will not do what you expect. – Galik Sep 22 '14 at 06:12
  • Ah ok, I see. Still learning :-) –  Sep 22 '14 at 06:12
  • I'll make up some simple examples – Galik Sep 22 '14 at 06:13
  • That'd be awesome, thanks! –  Sep 22 '14 at 06:15

1 Answers1

2

Here are some examples to get you started:

To copy everything:

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
}

Keep track of the line numbers:

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
}

Then you just need to figure how to only output when the line number (count) is what you want.

TIP: Never check for eof() in the while() loop. Here is why: http://www.parashift.com/c++-faq/istream-and-eof.html

Galik
  • 47,303
  • 4
  • 80
  • 117
  • I'm working on implementing this into my code, but am getting the following error when I attempt to compile "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: note: candidates are: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits]" Don't know that this matters, but I'm coding on a Linux system. –  Sep 22 '14 at 06:45
  • @Beth Are you using a fairly old version of the C++ compiler? Is this relevant? http://stackoverflow.com/questions/6323619/c-ifstream-error-using-string-as-opening-file-path – Galik Sep 22 '14 at 06:50
  • I'm using a remote login Linux program that allows us to use our school's server, and it is using the compiler C++ 4.1.2 20080704 (that's what is displays when I type g++ --version on the command line anyway). I read that you can use the -std=c++0x compiler flag, but not sure where to do this, like does is go in my code or the command line, or if it would solve my problem? –  Sep 22 '14 at 07:02
  • I edited the original question to reflect my most recent code and the compiler error I'm getting. –  Sep 22 '14 at 08:10
  • 1
    You're line `in1.open();` doesn't call `open()` properly and is unnecessary because the previous line opens the file (see how you open in2 for the correct way). ALSO you need to reset `count` to `1` before reading the second file! – Galik Sep 22 '14 at 08:42
  • Well that was simple enough, that got rid of all the errors. Now just to get it to run, I'm getting a "Permission denied" error when I try and run it :-/ –  Sep 22 '14 at 08:50
  • can you post the exact error (with any usernames etc.. removed if you like)? – Galik Sep 22 '14 at 09:00
  • I was able to correct that error by going in and changing the file's permissions (I guess by default I didn't have execute permissions, only read and write). However, I'm about to update the original post with the runtime error I'm getting now. –  Sep 22 '14 at 09:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61660/discussion-between-galik-and-beth-tanner). – Galik Sep 22 '14 at 09:11