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 :)

Dani AI

Generated

A concise summary and practical follow-up to the thread: the goal is pairwise merging of two text files so each output line looks like first,second. The symptom in Post #5 (many repeated 2 values) came from the nested while loops — the inner loop consumed the entire first file for each line of the second file. As suggested, keep both streams open and read them in lock-step.

A robust pattern is to read each file with getline in the loop condition so failures stop the loop immediately; also strip a trailing \r if files come from Windows. For example:

while (getline(f1, a) && getline(f2, b)) {
    if (!a.empty() && a.back() == '\r') a.pop_back();
    if (!b.empty() && b.back() == '\r') b.pop_back();
    out << a << ',' << b << '\n';
}

If file lengths differ, decide how to proceed: stop at the shorter file (the snippet above), or continue and supply a default/empty field for missing values. To continue and write remaining lines from the longer file:

while (getline(f1, a)) out << a << ",\n";
while (getline(f2, b)) out << "," << b << "\n";

Notes and cautions: check that streams opened successfully before processing; use a temporary output file and atomically replace the original when modifying in-place (opening the original with ios::app only appends at the file end and will not insert text at the end of each existing line). If the second file is simply the line index, an even simpler solution is to read the first file once and append an incrementing counter to each line instead of storing a separate file. Finally, as implied, confirm the exact input format (empty lines, whitespace, encoding) before choosing the final approach.

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.

: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.

, 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.

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.