Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将输出写入文件的不同列_C++ - Fatal编程技术网

C++ 将输出写入文件的不同列

C++ 将输出写入文件的不同列,c++,C++,您好,我想知道是否有任何函数库可以让我方便地做到这一点,或者您是否对我如何在不编写一行又一行代码的情况下优雅地做到这一点有任何建议(这就是我最终使用ofstream所做的) 像这样的。我需要比较一个QM问题的一百个不同的本征函数,我不想为每个alpha值编写一个文件,这也会使绘图更容易 我在没有任何可用结果的情况下使用google,感谢您的帮助:)如果您的算法允许您这样做,请重构循环以按行编写: Loop over j { Loop over i { Evaluate f

您好,我想知道是否有任何函数库可以让我方便地做到这一点,或者您是否对我如何在不编写一行又一行代码的情况下优雅地做到这一点有任何建议(这就是我最终使用ofstream所做的)

像这样的。我需要比较一个QM问题的一百个不同的本征函数,我不想为每个alpha值编写一个文件,这也会使绘图更容易


我在没有任何可用结果的情况下使用google,感谢您的帮助:)

如果您的算法允许您这样做,请重构循环以按行编写:

Loop over j {
    Loop over i {
        Evaluate f(i*alpha, j);
        Write f to column i, <TAB>;
    }
    Write <CR><LF>
}
在j上循环{
绕一圈{
评估f(i*alpha,j);
将f写入第i列;
}
写
}

您的问题是对齐的表格输出,即直观地显示列,还是能够写入第四个字段,然后写入第二个字段

前:最小C++解决方案使用>/COD> 假设您有:

#include <iostream>
#include <vector>
#include <iomanip>

struct Date     { int year, month, day;
                  Date(int year, int month, int day)
                      : year(year), month(month), day(day) {}
                };
struct Time     { int hour, minute, second;
                  Time (int hour, int minute, int second)
                      : hour(hour), minute(minute), second(second){}
                };
struct Birthday { Date date;
                  Time time;
                  Birthday (Date date, Time time)  : date(date), time(time) {}
                };

std::ostream& operator<< (std::ostream &ofs, Time const &rhs) {
    using std::setw;
    return ofs << std::setfill('0')
               << setw(2) << rhs.hour << ':'
               << setw(2) << rhs.minute << ':'
               << setw(2) << rhs.second;
}
std::ostream& operator<< (std::ostream &ofs, Date const &rhs) {
    using std::setw;
    return ofs << std::setfill('0')
               << setw(4) <<  rhs.year << '-'
               << setw(2) << rhs.month << '-'
               << setw(2) << rhs.day;
}
std::ostream& operator<< (std::ostream &ofs, Birthday const &rhs) {
    return ofs << rhs.date << ' ' << rhs.time;
}


struct Dude {
    std::string first_name;
    std::string last_name;
    Birthday    birthday;
    Dude (std::string const &f, std::string const &l, Birthday const &b)
        : first_name(f), last_name(l), birthday(b) {}
};
#包括
#包括
#包括
结构日期{整数年、月、日;
日期(整数年、整数月、整数日)
:年、月、日{}
};
结构时间{整小时,分钟,秒;
时间(整数小时、整数分钟、整数秒)
:时(小时)、分(分钟)、秒(秒){}
};
结构生日{日期;
时间;
生日(日期,时间):日期(日期),时间(时间){}
};

std::ostream&stream的操作员
应该能够在没有“行和行”代码的情况下完成这项工作。你能把你试过的东西贴出来吗?@chad不幸的是,还没有,我在火车上用ipod,再过6个小时就不在家了,我想不出来。你能把你的数据合理地放在内存中的适当列中吗,然后在一次操作中将整个文件转储到一个文件中?@Chad:听起来像是我们在我的一份老工作中不得不对付的一个非常遗留的DBMS。不要那样做,这会引起很多挫折。举个例子:当一个旧版本的平面文件的布局与当前版本的数据库不同时,如何读取它?然后我需要以某种方式回到Columns,因为gnuplot的设置方式,它只能读取数据Column vs Columnso,您将内存中的所有内容保存在一个逻辑位置,然后在一次操作中将其全部写入文件;)我的问题是简单地将原始数据(双倍)放在不同的列中。到家后我会尝试你的方法,希望我可以尝试最后一部分,而不必为我的数据构建结构。@Chad:我假设有一系列数据行。iostream是连续的流,因此适合它们。我不确定你到底需要什么。@user948652:我想你是@Chad?如果您从不同的帐户发帖,则会造成混乱;)不,他不是我。:)他只是更清楚地表达了同样的想法。
#include <iostream>
#include <vector>
#include <iomanip>

struct Date     { int year, month, day;
                  Date(int year, int month, int day)
                      : year(year), month(month), day(day) {}
                };
struct Time     { int hour, minute, second;
                  Time (int hour, int minute, int second)
                      : hour(hour), minute(minute), second(second){}
                };
struct Birthday { Date date;
                  Time time;
                  Birthday (Date date, Time time)  : date(date), time(time) {}
                };

std::ostream& operator<< (std::ostream &ofs, Time const &rhs) {
    using std::setw;
    return ofs << std::setfill('0')
               << setw(2) << rhs.hour << ':'
               << setw(2) << rhs.minute << ':'
               << setw(2) << rhs.second;
}
std::ostream& operator<< (std::ostream &ofs, Date const &rhs) {
    using std::setw;
    return ofs << std::setfill('0')
               << setw(4) <<  rhs.year << '-'
               << setw(2) << rhs.month << '-'
               << setw(2) << rhs.day;
}
std::ostream& operator<< (std::ostream &ofs, Birthday const &rhs) {
    return ofs << rhs.date << ' ' << rhs.time;
}


struct Dude {
    std::string first_name;
    std::string last_name;
    Birthday    birthday;
    Dude (std::string const &f, std::string const &l, Birthday const &b)
        : first_name(f), last_name(l), birthday(b) {}
};
int main () {
    using std::setw;

    std::vector<Dude> d;
    d.push_back (Dude("John", "Doe",        Birthday(Date(1980,12,11),Time(6,45,0))));
    d.push_back (Dude("Max",  "Mustermann", Birthday(Date(1980,12,11),Time(6,45,0))));

    std::cout << std::left;

    // Output a fancy header.
    std::cout << std::setfill(' ')
              << setw(24) << "<last name>" << "| "
              << setw(16)  << "<first name>" << "| "
              << "birthday" << '\n';

    // Data output follows. Note: No lines of lines and code.
    for (std::vector<Dude>::iterator it=d.begin(), end=d.end(); it!=end; ++it) {
        std::cout << std::setfill(' ')
                  << setw(24) << it->last_name << "| "
                  << setw(16) << it->first_name << "| "
                  << it->birthday  << '\n';
    }

}