生成ngc2000.dat的代码(Qt源码):
csv的格式读一读代码就懂了,照着写解码器也很简单。
#include <QDebug>
#include <QFile>
#include <QString>
#include <QtCore/qmath.h>
QString csvfile = "ngcic.csv";
bool dumpNGC(const QString& catNGC)
{
bool isIc;
int nb;
float ra, dec;
unsigned int type;
float mag; // Apparent magnitude
float angularSize; // Angular size in degree
int totalRecords=0;
const QString FILE_PATH(csvfile);
QFile csvFile(FILE_PATH);
QStringList CSVList;
CSVList.clear();
if (csvFile.open(QIODevice::ReadWrite))
{
QTextStream stream(&csvFile);
while (!stream.atEnd())
{
CSVList.push_back(stream.readLine());
}
csvFile.close();
}
/**/
QFile in(catNGC);
if (!in.open(QIODevice::WriteOnly))
return false;
QDataStream ins(&in);
ins.setVersion(QDataStream::Qt_4_5);
/**/
Q_FOREACH(QString str, CSVList)
{
isIc = str.split(",")[0] == "NGC" ? false : true;
nb = str.split(",")[1].toInt();
ra = str.split(",")[2].toFloat();
dec = str.split(",")[3].toFloat();
mag = str.split(",")[4].toFloat();
angularSize = str.split(",")[5].toFloat();
type = str.split(",")[6].toInt();
ins << isIc << nb << ra << dec << mag << angularSize << type;
// qDebug()<< isIc << nb << ra << dec << mag << angularSize << type;
++totalRecords;
}
in.close();
//qDebug() << "Dumped" << totalRecords << "NGC records";
return true;
}
void MainWindow::on_pushButton_2_clicked()
{
dumpNGC("ngc2000.dat");
}
本文提供了一段使用Qt的C++代码,该代码从CSV文件中读取天体数据,并将其转换为特定格式,最终生成ngc2000.dat文件。代码展示了如何操作文件、解析CSV数据并进行数据流的序列化。
659

被折叠的 条评论
为什么被折叠?



