hi :) is there a possible way where in we can combine two rows for an example i have this code:

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

using namespace std;

int main()
{
    ofstream outfile;
    outfile.open("1stRow.txt");
    for(int i=1;i<10;i++)
        outfile<<i<<endl;
    outfile.close();

    ofstream outfile2;
    outfile2.open("2ndRow.txt");
    for(int j=2;j<11;j++)
        outfile2<<j<<endl;
    outfile2.close();


    //return 0;
}

Like the output file would be
1,2
2,3
3,4
4,5
5,6
6,7
7,8
8,9
9,10

in another file :), its like you concatenate two strings...
thank you in advance guys :)

Recommended Answers

All 6 Replies

It is possible. What do you think should be the strategy? It looks like a homework question, so we'll only provide hints. But first think of something yourself and we'll help you build on it.

Also, please specify what your input files look like so that we have an idea of how the merge is happening.

@NP-complete:thank you for replying!
oh by the way, the row should column. 1stROW should be 1stColumn sorry.
no it is not a homework,it is a project i am working on and just thought this one is the easiest way to get it done.

the idea of the project is have the count(number) of each line of a textfile beside each line.

say, on the first line "5,1" where "5" is any number the user inputs and and "1" indicates its the 1st line of the textfile. i put it in another textfile because i'll be using it elsewhere. the second textfile is actually the number of lines of the first textfile.

there,i hope i uhh answered the question,sincerely its no homework.:)

There is no reason why you cant have both streams open at the same time and continue to extract from both so long as there is data in each. For example (pseudocode):

stream1 = open(1strow.txt);
stream2 = open(2ndrow.txt);

while (stream1.good and stream2.good) 
   c1 = stream1.extract
   c2 = stream2.extract
   output c1 + ',' + c2
end while

Of course, to write to a new file you can either do that programmatically (within your code) our you can redirect the output of your program to a file using OS facilities.

@L7Sqr, i somehow did your pseudocode but i have yet to know how to loop the "how i can append on each line of the 1stcolumn" so far this is what i get.,is it not that when we say append,it is not erasing the characters,but just "adding" characters?

            outline.open("outline.txt");
             ifstream file("1stCol.txt");
             ifstream sc("2ndCol.txt");
            if (file&&sc)
            {
                 string line,line2;
                while(getline(sc,line2))
                while (getline(file, line))

                outline<<line<<line2<<endl;

             }outline.close();

i get:
1,2
2,2
3,2
4,2
5,2
6,2
7,2
8,2
9,2

thanks!:)

You want something more along the lines of:

ofstream out("output.txt");
ifstream file1("1stCol.txt"), file2("2ndCol.txt");

while (file1.good () && file2.good ()) {
   string c1, c2;
   getline (file1, c1);
   getline (file2, c2);
   // TODO: check that you actually read something valid here...
   out << c1 << ',' << c2 << std::endl;
}

Thew while loops you have set up will continue to read until the stream is exhausted. You will ignore all lines but the last thing returned by getline.

@L7Sqr thank you very much for replying!:) i therefore declare this discussion is solved.:) thanks!:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.