3

I've run into another problem in Qt, I can't seem to figure out how to write on a specific line on a text file with QFile. Instead, everything is erased written at the beginning. So with the given information, how would I write to a specific line in QFile?

Here are two functions.

  1. The first function searches a file, and then gets two variables. One that finds the next empty line, one that gets the current ID number.
  2. Second function is supposed to write. But I've looked for documentation on what I need, I've googled it and tried many searches to no avail.

Function 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

}

The function above reads the file, which looks like this.

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

And the function gets two bits of information that I figured I might need. I'm not too familiar with QFile and searching, but I thought that I'd need these variables:

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

So I passed in those variables to the next function, at the end of function 1. Write(x, currentID);

Function 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";
}

I've left out any attempts at fixing the problem myself, all this function does is replace all the contents of the text file with "HelloWorld".

Does anyone know how to write on a specific line, or at least go to the end of the file and then write?

NG_
  • 6,895
  • 7
  • 45
  • 67
Gabriel
  • 3,039
  • 6
  • 34
  • 44

2 Answers2

5

If the line you want to insert into the file is always the last line (as the function 1 suggest) you can try to open the file in append mode using QIODevice::Append in your Write method.

If you want to insert a line in the middle of the file, I suppose an easy way is to use a temp file (or, if it is possible, to load the lines into a QList, insert the line and write the list back to the file)

Gianluca
  • 3,227
  • 2
  • 34
  • 35
  • I might have to copy it all and then put into the new file. I tried using append but I'm not to sure how to use it ^_^ – Gabriel Apr 06 '12 at 20:01
  • open the file with mFile.open(QFile::Append | QFile::Text) – Gianluca Apr 06 '12 at 20:02
  • and then what? There still aren't any functions that I know of to work on 1 line... – Gabriel Apr 06 '12 at 20:03
  • and then write the line (your `out << "HelloWorld"` line). Opening the file in append mode set the current pointer of the file to the end of it, which is what you don't do in your `Write` method and so you write the new line at the beginning of the file. – Gianluca Apr 06 '12 at 20:10
1
    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();

The above code snippet will append the text "The magic number is: 4" , at the end of the file.

Soumya Kundu
  • 792
  • 6
  • 6