Qt, QFile write on a specific line
I faced another problem in Qt, I cannot figure out how to write on a specific line of a text file using QFile
. Instead, everything is erased at the beginning. So, with the given information, how should I write to a specific line in QFile
?
There are two functions.
- The first function searches for a file and then gets two variables. The one that finds the next empty string, which gets the current ID number.
- The second function is supposed to write. But I have searched the documentation for what I need, I have searched for 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 above function reads a file that 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 receives two bits of information that I figured I might need. I'm not too familiar with QFile
and searched, but I thought I needed 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 these 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 have not had any attempts to solve the problem myself, this whole function replaces the entire 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?
source to share
If the line you want to insert into the file is always the last line (as function 1 suggests), you can try opening the file in append mode using QIODevice :: Append in the Write method.
If you want to insert a line in the middle of a file, I suppose an easy way is to use a temporary file (or if possible, load the lines into a QList, insert the line and write back to the file)
source to share
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 add the text "Magic Number: 4" at the end of the file.
source to share