0

I'm trying to read a file which has names of servers on individual lines.

I want the output in the text browser to be one line as .*<servername>|<servername>|<servername>.*

I've tried both readings directly to the text browser, and reading to an array and outputting from the array. Both methods result in the text browser displaying each item on its own line. I want all output on the same line. Perhaps it's the append method or the .readLine method that's causing it?

Here's my code:

void MainWindow::on_pushButton_clicked()
{   
    QString myfile=QFileDialog::getOpenFileName(
                this,
                tr("Open File"),
                "C://",
                "Text File (*.txt)"
                );

    QFile file(myfile);
    if(!file.open(QIODevice::ReadOnly))
        QMessageBox::information(0,"info",file.errorString());

    QTextStream in(&file);
    ui->textBrowser->append(".*");

    while (!file.atEnd())
    {
        QString line = file.readLine();
        ui->textBrowser->append(line);
        ui->textBrowser->append("|");
    }

    file.close();
}
Mathieu
  • 8,840
  • 7
  • 32
  • 45
Austin
  • 11
  • 1

1 Answers1

0

The return of readLine includes the newline character in the returned buffer. So when you write it out to the textBrowser that includes a newline.

See qiodevice::readLine docs for more information.

mascoj
  • 1,313
  • 14
  • 27
  • So how can I read a line from the text file without returning the newline character? I tried file.read() instead but that doesnt work. – Austin Oct 04 '16 at 21:30
  • line.remove(QRegExp("[\n\t\r]")); // just after you read the line – Nathaniel Johnson Oct 04 '16 at 22:23
  • I ended up doing exactly what you have above by googling ways to remove carriage returns from data read in by readline from a Qfile. I wish I could mark your response as an answer as you were far more helpful and kind than Kuba Ober. The elitist attitude some people portray towards IT knowledge and those who ask questions can be stifling. It's nice to have people such as yourself who remember what it was like to be new. Thanks for your help – Austin Oct 07 '16 at 19:45