C++ QSettings:如何从INI文件读取数组

C++ QSettings:如何从INI文件读取数组,c++,arrays,qt,ini,qsettings,C++,Arrays,Qt,Ini,Qsettings,我想从INI文件中读取逗号分隔的数据。我已经在这里读到: …逗号被视为分隔符,QSettings value函数将返回QStringList 但是,INI文件中的数据如下所示: norm-factor=<<eof 1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0 1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0 1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0 1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0 1.0,1.0,1

我想从INI文件中读取逗号分隔的数据。我已经在这里读到:

…逗号被视为分隔符,QSettings value函数将返回QStringList

但是,INI文件中的数据如下所示:

norm-factor=<<eof
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
eof

或者我必须以另一种方式解析它吗?

换行符是一个问题,因为INI文件使用换行符作为自己的语法。
Qt似乎不支持您的换行类型换行是一个问题,因为INI文件使用换行符作为自己的语法。
Qt似乎不支持您的行延续类型Ronny Brendel的回答是正确的…我只是添加了解决上述问题的代码…它创建了带有更正数组的临时INI文件:

/**
 * @param src source INI file
 * @param dst destination (fixed) INI file
 */
void fixINI(const QString &src, const QString &dst) const {

  // Opens source and destination files
  QFile fsrc(src);
  QFile fdst(dst);
  if (!fsrc.open(QIODevice::ReadOnly)) {
    QString msg("Cannot open '" + src + "'' file.");
    throw new Exception(NULL, msg, this, __FUNCTION__, __LINE__);
  }
  if (!fdst.open(QIODevice::WriteOnly)) {
    QString msg("Cannot open '" + dst + "'' file.");
    throw new Exception(NULL, msg, this, __FUNCTION__, __LINE__);
  }

  // Stream
  QTextStream in(&fsrc);
  QTextStream out(&fdst);

  bool arrayMode = false;
  QString cache;
  while (!in.atEnd()) {

    // Read current line
    QString line = in.readLine();

    // Enables array mode
    // NOTE: Clear cache and store 'key=' to it, without '<<eof' text
    if (arrayMode == false && line.contains("<<eof")) {
      arrayMode = true;
      cache = line.remove("<<eof").trimmed();
      continue;
    }

    // Disables array mode
    // NOTE: Flush cache into output and remove last ',' separator
    if (arrayMode == true && line.trimmed().compare("eof") == 0) {
      arrayMode = false;
      out << cache.left(cache.length() - 1) << "\n";
      continue;
    }

    // Store line into cache or copy it to output
    if (arrayMode) {
      cache += line.trimmed() + ",";
    } else {
      out << line << "\n";
    }
  }
  fsrc.close();
  fdst.close();
}

Ronny Brendel的回答是正确的…我只添加了解决上述问题的代码…它使用更正的数组创建临时INI文件:

/**
 * @param src source INI file
 * @param dst destination (fixed) INI file
 */
void fixINI(const QString &src, const QString &dst) const {

  // Opens source and destination files
  QFile fsrc(src);
  QFile fdst(dst);
  if (!fsrc.open(QIODevice::ReadOnly)) {
    QString msg("Cannot open '" + src + "'' file.");
    throw new Exception(NULL, msg, this, __FUNCTION__, __LINE__);
  }
  if (!fdst.open(QIODevice::WriteOnly)) {
    QString msg("Cannot open '" + dst + "'' file.");
    throw new Exception(NULL, msg, this, __FUNCTION__, __LINE__);
  }

  // Stream
  QTextStream in(&fsrc);
  QTextStream out(&fdst);

  bool arrayMode = false;
  QString cache;
  while (!in.atEnd()) {

    // Read current line
    QString line = in.readLine();

    // Enables array mode
    // NOTE: Clear cache and store 'key=' to it, without '<<eof' text
    if (arrayMode == false && line.contains("<<eof")) {
      arrayMode = true;
      cache = line.remove("<<eof").trimmed();
      continue;
    }

    // Disables array mode
    // NOTE: Flush cache into output and remove last ',' separator
    if (arrayMode == true && line.trimmed().compare("eof") == 0) {
      arrayMode = false;
      out << cache.left(cache.length() - 1) << "\n";
      continue;
    }

    // Store line into cache or copy it to output
    if (arrayMode) {
      cache += line.trimmed() + ",";
    } else {
      out << line << "\n";
    }
  }
  fsrc.close();
  fdst.close();
}

你试过了吗?为什么不呢?你能提供一个完整的例子,以便我们可以运行和修改它吗?听起来好像你没有按照你建议的方式打开它。试试看。这不是Qt的标准ini格式。可能,你不能用QSettings来阅读。我现在不能尝试。。。我会尽快尝试它,只要我在pc上与编译器,你显示的不是一个.ini文件。有人给了它一个.ini扩展名,但它是一个自定义格式。你试过了吗?为什么不呢?你能提供一个完整的例子,以便我们可以运行和修改它吗?听起来好像你没有按照你建议的方式打开它。试试看。这不是Qt的标准ini格式。可能,你不能用QSettings来阅读。我现在不能尝试。。。我会尽快尝试它,只要我在pc上与编译器,你显示的不是一个.ini文件。有人给了它一个INI扩展,但它是一个自定义格式。请考虑接受/检查标记答案和投票。请考虑接受/检查标记答案和投票。
QVariant(QString, "<<eof")
/**
 * @param src source INI file
 * @param dst destination (fixed) INI file
 */
void fixINI(const QString &src, const QString &dst) const {

  // Opens source and destination files
  QFile fsrc(src);
  QFile fdst(dst);
  if (!fsrc.open(QIODevice::ReadOnly)) {
    QString msg("Cannot open '" + src + "'' file.");
    throw new Exception(NULL, msg, this, __FUNCTION__, __LINE__);
  }
  if (!fdst.open(QIODevice::WriteOnly)) {
    QString msg("Cannot open '" + dst + "'' file.");
    throw new Exception(NULL, msg, this, __FUNCTION__, __LINE__);
  }

  // Stream
  QTextStream in(&fsrc);
  QTextStream out(&fdst);

  bool arrayMode = false;
  QString cache;
  while (!in.atEnd()) {

    // Read current line
    QString line = in.readLine();

    // Enables array mode
    // NOTE: Clear cache and store 'key=' to it, without '<<eof' text
    if (arrayMode == false && line.contains("<<eof")) {
      arrayMode = true;
      cache = line.remove("<<eof").trimmed();
      continue;
    }

    // Disables array mode
    // NOTE: Flush cache into output and remove last ',' separator
    if (arrayMode == true && line.trimmed().compare("eof") == 0) {
      arrayMode = false;
      out << cache.left(cache.length() - 1) << "\n";
      continue;
    }

    // Store line into cache or copy it to output
    if (arrayMode) {
      cache += line.trimmed() + ",";
    } else {
      out << line << "\n";
    }
  }
  fsrc.close();
  fdst.close();
}