【问题标题】:Replacing a text in file in C++ using Qt使用 Qt 在 C++ 中替换文件中的文本
【发布时间】:2015-06-26 09:16:58
【问题描述】:

我正在使用 Qt 库,我正在尝试更改文件的内容。我想用 fname 替换存储在 tok2 中的文本。 更新代码:

QFile file(destPath);
if (file.open(QIODevice::ReadWrite | QIODevice::Text))
{
  QTextStream stream(&file);
  while (!stream.atEnd())
  {
    QString line = stream.readLine();
    QStringList tokenList = line.split("\t");        
    if ( tokenList.count() == 2 && (tokenList.at(0).endsWith("FILE",Qt::CaseInsensitive)))
    {   
      QString tok1 = tokenList.at(0).trimmed();    
      QString tok2 = tokenList.at(1).trimmed();
      QFileInfo relPath(tok2);
      QString fname = relPath.fileName();

        QString newLine = tok1.append(" ").append(fname);
        QString oldLine = tok1.append(" ").append(tok2);
        qDebug() << "Original line: " << oldLine << "New line" << newLine;
        QTextStream in(&file);

        while (!in.atEnd())
        {
          QString line = in.readLine();
          QString outline = line.replace(QString(oldLine), QString(newLine));
          in << outline;
        }
      }
    }                       
  }
}

tok2 的原始内容格式为 ../something/filename.ext,我必须用 filename.ext 替换它,但上面的代码没有用 fname 替换 tok2 的内容,总之我无法在此写入文件。

【问题讨论】:

  • 你的问题是?
  • fname 的内容不会复制到文件中。
  • 你在哪里写回文件?
  • 您的示例没有显示任何尝试写入文件的代码。
  • @user,为什么会这样? textQString,它甚至不知道它来自一个文件。

标签: c++ qt io


【解决方案1】:

我的解决方案非常适合我:

// Open file to copy contents
QFile file(srcPath);
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
    // Open new file to write
    QFile temp(destPath);
    if (temp.open(QIODevice::ReadWrite | QIODevice::Text))
    {
          QTextStream stream(&file);
          QTextStream out(&temp);
          while (!stream.atEnd())
          {
                QString newLine;
                //do stuff
                out << newLine  << "\n";
          }
      temp.close();
     }
     file.close();
 }

【讨论】:

    【解决方案2】:

    你把事情弄得太复杂了。

    const QString doStuff(const QString &str)
    {
        // Change string however you want
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication app(argc, argv);
    
        const QString filePath = "/home/user/test.txt";
        QTextCodec *codec = QTextCodec::codecForLocale();
    
        // Read file
        QFile file(filePath);
        if (!file.open(QFile::ReadOnly)) {
            qDebug() << "Error opening for read: " << file.errorString();
            return -1;
        }
        QString text = codec->toUnicode(file.readAll());
        file.close();
    
        text = doStuff(text);
    
        // Write file
        if (!file.open(QFile::WriteOnly)) {
            qDebug() << "Error opening for write: " << file.errorString();
            return -2;
        }
        file.write(codec->fromUnicode(text));
        file.close();
    
        return 0;
    }
    

    如果您的文件大小小于您的 RAM 容量,则运行速度足够快。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 2012-08-06
      • 1970-01-01
      相关资源
      最近更新 更多