开发者

c++ Output Format to Console Based on User's Define Number Of Columnns

I like to display data information to dos console based on user's template or ini file. Example, UserTemplate.txt

ParamA=yes
ParamB=yes
ParamC=yes
ParamD=yes
ParamE=yes
ParamF=no
ParamG=yes
ParamH=no
..
..

My program will read this UserTemplate.txt and what parameter user wants to display to dos console.

    while (!file_opc.eof())
{
    std::vector<std::string> v; 

    file_opc.getline(str,200);
    cout <<"\nline"<<开发者_开发技巧;str<<endl;
    if (strstr(str, "=") != NULL)
    {
        boost::algorithm::split_regex( v, str, boost::regex( "=|//" ) ) ;
        cout<<"Param="<<v.at(0)<<"\nFlag="<< v.at(1)<<endl;
        ParamNames.push_back(v.at(0).c_str());
        ParamFlags.push_back(v.at(1).c_str());
    }
}

Output format for number of Columns are variable based flags (yes/no) from user

ParamA    ParamB    ParamC    ParamD    ParamE   ParamG 
------    ------    ------    ------    ------   ------
 123        Ack      NewTx     24.0     Block     64QAM

Since ParamF and ParamH sets no NO. It won't display to dos console. Those "123", "Ack", "NewTx" etc.. are the vectors parsed from a data source. I need help how dump those ParamX to dos console based on UserTemplate.txt

Right now, I am hard-coded some parameters as follow.

//////////////////
cout<<"cRnti   trNum    ackNack   harqNum   RachM2   ReliTransF   MCS CW1   SINRPUSCH "<<endl;
cout<<"=====   =====   =======   =======   ======   ==========   =======   ========= "<<endl;
SetConsoleTextAttribute(hhConsole, 15);
for (unsigned int i=0;i<RecordInMemory;i++)
{
    ss<<setw(5)<<cRnti[i]<<setw(8)<<trNumCw1[i]<<setw(8)<<ackNackDtxCw1[i]<<setw(9)<<harqNumCw1[i]<<setw(10)<<pdcchOrRachM2[i]<<setw(10)<<reliableTransmissionFlag[i]<<setw(12)<<mcsIndexCw1[i]<<setw(12)<<sinrPusch[i];
    cout<<ss.str()<<endl;
    ss.str(std::string());
}

If you have better idea to handle my case, please shred the light, I appreciate. It saves me a lot of times. If you know there is already solution in this site, please give me a link. Thanks in advance.


I would probably create a small data structure that tells how (and if) to display each column:

struct column_display {
    bool display;
    int width;

    column_display(bool d) : display(d), width(0) {}
};

Then we'd add some code to read each name:display pair from the file:

std::istream &operator>>(std::istream &is, std::pair<std::string, column_display> &c) { 
    std::string name, value;
    std::getline(is, name, '=');
    std::getline(is, value);

    c.first = name;
    c.second.display = (value == "yes");
    return is;         
}    

Then you'd read your data for the columns into these structures (and presumably add the correct widths somehow or other, either stored internally or from another external source):

std::map<std::string, column_display> params((std::istream_iterator(params)), 
                                             std::istream_iterator());

Finally, you'd write out the data using the specified parameters:

class display { 
    record_t const &r;
    std::ostream &out;
public:
    display(record_t const &record, std::ostream &o) : r(record), out(o) {}
    display &operator()(std::pair<std::string, column_display> const &d) { 
        if (d.second.display)
           out << d.second.width << r.fields[d.first];
    }
}

for (current_record = 0; current_record<record_count; current_record++) {
   display display_record(records[current_record], std::cout);
   std::for_each(params.begin(), params.end(), display_record);
}

At least for the moment, this assumes your "record" type is also a map (or something on that order) that lets you look up the data based on the same column name that's read from the userTemplate.txt file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜