1.代码
#include <QCoreApplication> #include <QFile> #include <QTextStream> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 打开CSV文件 QFile file("data.csv"); if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) { qDebug() << "无法打开文件!"; return a.exec(); } // 创建文本流并设置编码 QTextStream stream(&file); stream.setCodec("UTF-8"); // 写入CSV文件 stream << "姓名,年龄,性别" << endl; stream << "张三,20,男" << endl; stream << "李四,25,女" << endl; // 移动文件指针到文件开头 file.seek(0); // 读取CSV文件 QString line; while (!stream.atEnd()) { line = stream.readLine(); QStringList fields = line.split(","); // 使用读取到的字段进行相应的操作 qDebug() << "姓名: " << fields.at(0); qDebug() << "年龄: " << fields.at(1); qDebug() << "性别: " << fields.at(2); } // 关闭文件 file.close(); return a.exec(); }
标签:CSV,Qt,argc,int,argv,include From: https://www.cnblogs.com/judes/p/17611235.html