Sorry to bother you all with a similar one as my previous.
I wrote below program for the purpose of, first reading 2 files. Think of these 2 files as the 2 columns of a data table. That means, 1st column is on the 1st file and the 2nd column is on the other file. On execution, the program must combine these 2 data columns(2 files) into a neat table( with solid 2 columns, of course), and write back it into a 3rd file.
My problem is that, the 2nd column doesn’t get printed in a straight column on the 3rd file. They get wiggled (If you can please test the program). I tried to solve this problem by inserting white spaces into the strings to align all of them along a single vertical line, but it didn’t worked because I regarded the lengths of all characters as the same, which is not the reality. I can’t see any way to do this aligning right, so can you please help?
Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
void adjoin();
void rprtprint();
void main()
{
adjoin();
rprtprint();
}
void adjoin()
{
char* f1name = "LFILE.dat";
char* f2name = "RFILE.dat";
char* f3name = "LRFILE.dat";
ifstream tfile1(f1name, std::ios::in);
ifstream tfile2(f2name, std::ios::out);
ofstream ofile(f3name);
string f1str, f2str;
vector <string> TRL;
int fullLen = 45, freeLen=0, cnt=0;
// read & store
while(!tfile1.eof()){
cnt++;
getline(tfile1, f1str);
getline(tfile2, f2str);
freeLen = fullLen - f2str.length();
TRL.push_back(f1str);
// attempt to put even spaces. but isn't working exactly because of the various lengths of
// characters
while(freeLen>0){
TRL.push_back(" ");
freeLen--;
}
TRL.push_back(f2str);
TRL.push_back(".");
}
// write
int i = 0;
while(i < TRL.size()){
ofile<< TRL[i];
i++;
}
}
void rprtprint()
{
char* ifname = "LRFILE.dat";
char* ofname = "PRINTFILE.dat";
ifstream ifile(ifname, std::ios::in );
ofstream ofile(ofname, std::ios::out );
string fstr;
while(!ifile.eof()){
getline(ifile, fstr, '.');
ofile<< fstr;
ofile<< "\n";
}
}