简体   繁体   中英

QT, Write in new line in a file

I have written a code to read from line_Edit in GUI, and write in a file. The code reads the text from line edit and writes the same in the file, but they are printed continuously without any spaces, I want to print the texts written in the line edit in different lines. The file has written text, just want to replace first word of each line by the user entered words.

Code to write in the file:

void MainWindow::on_write_btn_clicked(){
    QString str, str2, lin;
    str = ui->lineEdit->text();
    str2 = ui->lineEdit2->text();
    QFile file1("sample.txt");
    if(file1.open(QIODevice::ReadWrite | QIODevice::Text)){
        QTextStream out(&file1);
        out << str;
        lin = out.readLine();
        out << str2;
        file1.seek(30);
        file1.close();
    }
    else
        return;
}

File in which we want to write: 我们要写入的文件

If you want the next string to be in a new line in the file you should add the new line character(s) to the stream \\n . Referring to your code you should put:

out << str << '\n' << str2;

which would make the contents of str and str2 appear in consecutive lines.

Instead of the above you could also use the endl manipulator from QTextStream :

out << str << endl << str2;

For this to work properly you need to be opening the file with QIODevice::Text and assure that the endl you are specifying actually comes from QTextStream (not std ) Also note that since you probably only want to write your file there is no need in opening it with ReadWrite option, WriteOnly should be enough.

EDIT according to further details:

To substitute first word from each line of the file you could do the following. Open two files, one which will be read and the second to write the modified data. After iterating though all the lines close the files, remove the original one and rename the output file to replace the original one. Sample implementation:

QFile fileIn("textIn.txt"), fileOut("textOut.txt");
fileIn.open(QFile::ReadOnly); // check result
fileOut.open(QFile::WriteOnly); // check result

QTextStream streamIn(&fileIn), streamOut(&fileOut);
const QChar delimeter = ' ';
while (!streamIn.atEnd())
{
    QStringList list = streamIn.readLine().split(delimeter);
    if (list.size() > 0) // in case of empty line
        list[0] = "substitutedText"; // here put the text you want to set
    streamOut << list.join(delimeter) << "\r\n"; // or endl
}
fileIn.close();
fileOut.close();

fileIn.remove(); // check result
fileOut.rename(QFileInfo(fileIn).absoluteFilePath()); // check result

Of course you could try doing the replacement on the original file opened with ReadWrite modifier and setting proper position inside the stream using seek . Although it could get tricky due to different lengths of the read and written data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM