c++根据用户定义的列数输出到控制台的格式

发布于 2024-10-30 01:53:15 字数 1911 浏览 2 评论 0原文

我喜欢根据用户的模板或ini文件将数据信息显示到dos控制台。 例子, UserTemplate.txt

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

我的程序将读取这个UserTemplate.txt以及用户想要显示到dos控制台的参数。

    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());
    }
}

列数的输出格式是来自用户的基于变量的标志(是/否),

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

因为 ParamF 和 ParamH 设置为“否”。它不会显示到 dos 控制台。 那些“123”、“Ack”、“NewTx”等是从数据源解析的向量。我需要帮助如何根据 UserTemplate.txt 将这些 ParamX 转储到 dos 控制台

现在,我对一些参数进行了硬编码,如下所示。

//////////////////
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());
}

如果您有更好的主意来处理我的案子,请粉碎光线,我很感激。它为我节省了很多时间。如果您知道该网站已经有解决方案,请给我一个链接。提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾忌 2024-11-06 01:53:16

我可能会创建一个小型数据结构,告诉如何(以及是否)显示每一列:

struct column_display {
    bool display;
    int width;

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

然后我们添加一些代码来读取文件中的每个名称:显示对:

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

然后您将列的数据读入这些结构(并且可能以某种方式添加正确的宽度,无论是内部存储还是来自另一个外部源):

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

最后,您将使用指定的参数写出数据:

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

至少目前,这假设您的“记录”类型是还有一个映射(或该顺序上的某些内容),可让您根据从 userTemplate.txt 文件中读取的相同列名查找数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文