0

My goal is to take the text from "filename" that I have in my main directory, take the odd lines and send them over the filenameA, and the even lines to filenameB. From there, I want to splice them back into a new file. How can I create a while loop to do so?

// This program splits a file into two files from main directory
// and then splices the original file with a new name. 

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

void pause();

int main()
{
    char filename[] = "Lab2Test.txt";
    char filenameA[] = "LabTest-FA.txt";
    char filenameB[] = "LabTest-FB.txt";

    ifstream origin(filename);
    ofstream fA(filenameA);
    ofstream fB(filenameB);

    if (! origin)  
    {
       cout << filename << " could not be opened." << endl;
       return -1;
    }

    string s;
    int i=0;
    while(getline(origin, s))
    {
        if(i % 2 == 1) //odd - write to LabTest-FA
            fA <<  s << endl;
        else
            fB << s << endl;
        i++;
    }
}

void pause()
{
    cin.sync();
    cout << "Press any key to continue..." << endl;
    cin.ignore();
}
Cody
  • 37
  • 6
  • 1
    You need to create two `std::ofstream` variables, just as you've done for `origin`... those should be the targets for your `<<` output. To read input, use `std::string line; while (std::getline(origin, line)) ...`. You don't need to check `.eof()` anywhere - just get rid of that. And to *call* your `pause` function just write `pause();` without the leading `void`. Further, learn to write your program a little at a time - e.g. just reading lines from `origin`, then maybe printing every second line to `std::cout` - that way you can write compilable code that doesn't get out of hand. – Tony Delroy Sep 16 '14 at 04:12

1 Answers1

1

First pick a book on C++ and learn a bit more about the language, you have lots of mistakes in the code. Here is a working program, the best I can come up with.

// This program splits a file into two files from main directory
// and then splices the original file with a new name. 

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void pause()
{
    cin.sync();
    cout << "Press any key to continue..." << endl;
    cin.ignore();
}

int main()
{
    char filename[] = "Lab2Test.txt";
    char filenameA[] = "LabTest-FA.txt";
    char filenameB[] = "LabTest-FB.txt";
    char filenew[] = "Lab2Test2.txt";

    ifstream origin(filename);
    fstream fA(filenameA, std::fstream::in | std::fstream::out | std::fstream::trunc);
    fstream fB(filenameB, std::fstream::in | std::fstream::out | std::fstream::trunc);
    ofstream fnew(filenew);

    if (!origin)  
    {
       cout << filename << " could not be opened." << endl;
       return -1;
    }

    string s;
    int i = 0;
    while( getline(origin, s) )
    {
        if(i % 2 == 1) // odd - write to LabTest-FA
            fA << s << endl;
        else // even - write to LabTest-FB
            fB << s << endl;
        i++;
    }

    fA.flush(); // write to disk
    fB.flush();

    fA.seekg(0, ios::beg); // rewind the files to the beginning
    fB.seekg(0, ios::beg);

    string s1, s2;
    while( getline(fB,s1) )
    {
        fnew << s1 << endl;
        if(getline(fA,s2))
            fnew << s2 << endl;
    }
    pause();
}
vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • I appreciate it. How would I go about splicing the two new text files together? @vsoftco – Cody Sep 16 '14 at 04:19
  • @Cody you mean interleaved or just end to end, and which file to start with for the output splice, even or odd? – WhozCraig Sep 16 '14 at 04:25
  • You close the files, open them again, then use 2 getlines in a while loop and write each result of the getline to the new file, try to figure it out by yourself as this is the best way to learn. – vsoftco Sep 16 '14 at 04:25
  • @WhozCraig Interleaved, and begin with the odd. – Cody Sep 16 '14 at 04:33