【问题标题】:How to write tabular data in .CSV file in C++?如何在 C++ 中的 .CSV 文件中写入表格数据?
【发布时间】:2019-10-10 03:12:13
【问题描述】:

我正在尝试在 .csv 文件中以表格形式写入。许多字符串变量(频率电压电流 S11 S22 S21 S12)包含大数据。
对于经验,

...等等...

首先我通过将“,”替换为“\n”将所有变量数据行转换为列,以便以垂直形式写入。 对于经验,

转换成

string voltage , freq, curt;
freq= tmp.query("*FMA?\n");
voltage = tmp.query("*VOL?\n");
curt=   tmp.query("*CURR?\n");
replace(begin(freq), end(freq), ',', '\n');
replace(begin(voltage ), end(voltage), ',', '\n');
replace(begin(curt), end(curt), ',', '\n');
filewrite << freq<< voltage <<curt; 

【问题讨论】:

  • *.cvs 还是 *.csv?
  • .csv(逗号分隔文件)
  • 您必须在',' 处分割每一行,并使用filewrite &lt;&lt; freq&lt;&lt; ' '&lt;&lt;voltage &lt;&lt;' '&lt;&lt;curt&lt;&lt;'\n'; 将值写入循环中
  • 能否请您简单解释一下如何分割线以及如何以表格形式获取最终数据?

标签: c++ csv tabular


【解决方案1】:

完成了。 用“,”分割,用token写成表格形式。

ofstream filewrite;
filewrite.open("............");
string voltage , freq;
freq= "A1, A2, A3";
voltage = "B1, B2, B3";

string delimiter = ",";
    size_t pos = 0;
    string token, token2;
    while ((pos = freq.find(delimiter)) != string::npos && (pos =voltage .find(delimiter)) != string::npos)
    {
        token = freq.substr(0, pos);
        token2 = voltage .substr(0, pos);
        filewrite << token << "," << token2 << endl;
        freq.erase(0, pos + delimiter.length());
        str2.erase(0, pos + delimiter.length());
    }

    filewrite << freq<<"," << voltage ;
filewrite.close();

}

【讨论】:

    猜你喜欢
    • 2013-09-16
    • 1970-01-01
    • 2019-02-02
    • 2020-10-17
    • 2014-08-18
    • 2018-03-01
    • 1970-01-01
    相关资源
    最近更新 更多