0

I am trying to create a new file with data from two different existing files. I need to copy the first existing file in it's entirety, which I have done successfully. For the second existing file I need to copy just the last two columns and append it to the first file at the end of each row.

Ex:

Info from first file already copied into my new file:

20424297 1092 CSCI 13500 B 3

20424297 1092 CSCI 13600 A- 3.7

Now I need to copy the last two columns of each line in this file and then append them to the appropriate row in the file above:

17 250 3.00 RNL

17 381 3.00 RLA

i.e. I need "3.00" and "RNL" appended to the end of the first row, "3.0" and "RLA" appended to the end of the second row, etc.

This is what I have so far:

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

int main() {

    //Creates new file and StudentData.tsv
    ofstream myFile;
    ifstream studentData;
    ifstream hunterCourseData;

    //StudentData.tsv is opened and checked to make sure it didn't fail
    studentData.open("StudentData.tsv");
    if(studentData.fail()){

       cout << "Student data file failed to open" << endl;
       exit(1);
    }


    //My new file is opened and checked to make sure it didn't fail
    myFile.open("file.txt");
    if(myFile.fail()){

        cout << "MyFile file failed to open" << endl;
        exit(1);

    }

    //HunterCourse file is opened and checked to make sure if didn't   fail
    hunterCourseData.open("HunterCourse.tsv");
    if(myFile.fail()){

        cout << "Hunter data file failed to open" << endl;
        exit(1);
    }

    // Copies data from StudentData.tsv to myFile
    char next = '\0';
    int n = 1;

        while(! studentData.eof()){

        myFile << next;
        if(next == '\n'){

            n++;
            myFile << n << ' ';

        }
        studentData.get(next);

    }


    return 0;
}

I am going bananas trying to figure this out. I'm sure it's a simple fix but I can't find anything online that works. I've looked into using ostream and a while loop to assign each row into a variable but I can't get that to work.

Another approach that has crossed my mind is just to remove all integers from the second file because I only need the last two columns and neither of those columns include integers.

3
  • 1
    1. It looks like you wan to get the last column of the second file; 2. Do you really need to use C++? If you're on Linux, you can use a lot of utils to achieve the goal, e.g. awk.
    – Mine
    Feb 29, 2016 at 2:41
  • Sounds like you want to copy the last two columns, not the last two rows. The rows are the acrossy things the columns are the uppie-downy things.
    – Galik
    Feb 29, 2016 at 3:19
  • Yeah thanks for the heads up. I meant columns. Feb 29, 2016 at 3:31

1 Answer 1

0

If you take a look at the seekg method of a file-stream, you'll note the second version allows you to implement the location to set an offset from (such as ios_base::end which sets the offset compared to the end of the file. With this you can effectively read backwards from the end of the a file. Consider the following

int Pos=0;
while(hunterCourseData.peek()!= '\n')
{
    Pos--;
    hunterCourseData.seekg(Pos, ios_base::end);
}
//this line will execute when you have found the first newline-character from the end of the file.

Much better code is available at this Very Similar question

Another possibility is simply to find how many lines are in the file beforehand. (less fast, but workable), in this case one would simply loop though the file calling getline and increment a count variable, reset to the start, then repeat until reaching the count - 2. Though I wouldn't use this technique myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.