【问题标题】:Qt, QFile write on specific lineQt,QFile 写入特定行
【发布时间】:2012-04-06 19:16:21
【问题描述】:

我在 Qt 中遇到了另一个问题,我似乎无法弄清楚如何使用 QFile 在文本文件的特定行上写入内容。相反,一切都在开始时被擦除。 那么根据给定的信息,我将如何写入QFile 中的特定行?

这里有两个函数。

  1. 第一个函数搜索文件,然后获取两个变量。一个找到下一个空行,一个获取当前 ID 号。
  2. 第二个函数应该写。但是我已经查找了有关我需要什么的文档,我用谷歌搜索了它并尝试了许多搜索都无济于事。

功能 1


    QString fileName = "C:\\Users\\Gabe\\SeniorProj\\Students.txt";
    QFile mFile(fileName);
    QTextStream stream(&mFile);
    QString line;

    int x = 1; //this counts how many lines there are inside the text file
    QString currentID;

    if(!mFile.open(QFile::ReadOnly | QFile::Text)){
        qDebug() << "Could not open file for reading";
        return;
    }

    do {
        line = stream.readLine();
        QStringList parts = line.split(";", QString::KeepEmptyParts);

        if (parts.length() == 3) {
            QString id        = parts[0];
            QString firstName = parts[1];
            QString lastName  = parts[2];

            x++; //this counts how many lines there are inside the text file
            currentID = parts[0];//current ID number
        }
    }while (!line.isNull());

    mFile.flush();
    mFile.close();

    Write(x, currentID); //calls function to operate on file

}

上面的函数读取文件,如下所示。

1001;James;Bark
1002;Jeremy;Parker
1003;Seinfeld;Parker
1004;Sigfried;FonStein
1005;Rabbun;Hassan
1006;Jenniffer;Jones
1007;Agent;Smith
1008;Mister;Anderson

该函数获得了我认为可能需要的两个信息。我对QFile 和搜索不太熟悉,但我认为我需要这些变量:

int x;  //This becomes 9 at the end of the search.
QString currentID; //This becomes 1008 at the end of the search.

所以我将这些变量传递给下一个函数,在函数 1 的末尾。Write(x, currentID);

功能2


void StudentAddClass::Write(int currentLine, QString idNum){

    QString fileName = "C:\\Users\\Gabe\\SeniorProj\\Students.txt";
    QFile mFile(fileName);
    QTextStream stream(&mFile);
    QString line;

    if(!mFile.open(QFile::WriteOnly | QFile::Text)){
        qDebug() << "Could not open file for writing";
        return;
    }

    QTextStream out(&mFile);
    out << "HelloWorld";
}

我自己没有尝试解决这个问题,这个函数所做的只是用“HelloWorld”替换文本文件的所有内容。

有谁知道如何在特定的行上写,或者至少到文件末尾再写?

【问题讨论】:

    标签: c++ qt qfile


    【解决方案1】:

    如果您要插入文件的行始终是最后一行(如函数 1 建议的那样),您可以尝试在 Write 方法中使用 QIODevice::Append 以追加模式打开文件。

    如果你想在文件中间插入一行,我想一个简单的方法是使用一个临时文件(或者,如果可能的话,将这些行加载到一个 QList 中,插入该行并编写列表回文件)

    【讨论】:

    • 我可能必须将其全部复制,然后放入新文件中。我尝试使用 append 但我不确定如何使用它^_^
    • 用 mFile.open(QFile::Append | QFile::Text) 打开文件
    • 然后呢?我所知道的仍然没有任何功能可以在 1 行上工作...
    • 然后写下这一行(你的out &lt;&lt; "HelloWorld" 行)。以追加模式打开文件将文件的当前指针设置到文件末尾,这是您在 Write 方法中不做的事情,因此您在文件开头写入新行。
    【解决方案2】:
        QString fileName = "student.txt";
        QFile mFile(fileName);
    
        if(!mFile.open(QFile::Append | QFile::Text)){
            qDebug() << "Could not open file for writing";
            return 0;
        }
    
        QTextStream out(&mFile);
        out << "The magic number is: " << 4 << "\n";
    
        mFile.close();
    

    上面的代码 sn-p 将在文件末尾附加文本 "The magic number is: 4" 。

    【讨论】:

    • 以及如何将文本写入 1000 行的第二行?
    猜你喜欢
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 2017-10-15
    • 2011-03-26
    相关资源
    最近更新 更多