Thread: Table Problem2-(a different code)

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    22

    Table Problem2-(a different code)

    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";
    	}
              }

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Table Problem2-(a different code)

    Originally posted by kasun
    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?
    Well, if you are using proportional fonts as you allude to (I regarded the lengths of all characters as the same, which is not the reality) then you aren't going to get straight colums without some complex functions you probably don't want to get into at your level. Best thing is to change the font of the display to a nonproportional font (courier, lucinda, terminal, ...)

    You might be able to simply add \t (tab) between the columns, but each line of the first column must be similar in size.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Is this a solution?
    Code:
    int len = TRL.size();
    for (int i = 0; i < len; i++) {
        ofile << setw(50) << left << TRL[i];
    
        if (i % 2 == 1)  { // second column
            ofile << '\n';
        }
    }
    note: don't do any formatting when you store it to vector; do the formatting when you write it to a file
    Last edited by alphaoide; 01-16-2004 at 03:03 PM.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    22
    Yes, with that bit of code and using a nonproportional font I can make it.
    Thanks you guys a lot for your time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hash table code question
    By Boxknife in forum C Programming
    Replies: 20
    Last Post: 08-13-2008, 05:55 AM
  2. Relocatable code and Symbol Table
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-10-2002, 11:05 AM
  3. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  4. About the Morse code Converter
    By Amber_liam in forum C Programming
    Replies: 17
    Last Post: 05-29-2002, 08:35 AM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM