Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, September 2007
Re: Input with QTextStream, will not read an entire line.


Message 1 in thread

Have you already tried to use the QSettings class? An ini-file looks the
same as your file and you don't have to bother with all the file handling
and data reading stuff.

2007/8/31, Mark Stahler <markstahler@xxxxxxxxx>:
>
> Hi there. What I am having a problem with is a seeminly trivial task. I am
> using a simple gui interface to write a plain text settings file that will
> be read by a separate daemon/service program. The format of the config file
> is:
>
> server=stahler.dyndns.org
> protocol=sftp
> port=23
> username=anonymous
> password=
> backupdir=C:/Documents and Settings/Mark/Sync
> firefox=1
> path=C:/Documents and Settings/Mark/Application
> Data/Mozilla/Firefox/Profillt/bookmarks.html
>
> Output to the file works great. Ill include the code just in case:
>
> void MainWindow::save()
> {
> QFile file("settings.cfg");
> if (!file.open(QIODevice::WriteOnly))
> {
> cerr << "Cannot open file for writing: " << qPrintable( file.errorString())
> << endl;
> return;
> }
> QTextStream output(&file);
> // generalBox
> output << "server=" << server_lineEdit->text() << endl;
> output << "protocol=";
> if (ftp_radioButton->isChecked() == true)
> output << "ftp" << endl;
> else
> output << "sftp" << endl;
> output << "port=" << port_spinBox->value() << endl;
> output << "username=" << username_lineEdit->text() << endl;
> output << "password=" << password_lineEdit->text() << endl;
> output << "backupdir=" << backup_dir_lineEdit->text() << endl;
>
> // Firefox
> output << "firefox=" << firefoxBox->isChecked() << endl;
> output << "path=" << firefox_path_lineEdit->text() << endl;
> }
>
> The problem that I am having is when I need to input a line in my settings
> file that includes a space. My program is multi-platform and Windows needs
> spaces with path names. If I just use normal stream operators >> the QString
> that I am inputting is truncated at the first space. From the code you can
> see that I use the seperate() function to remove the prefix var data that is
> in the config file. That works but for my QStrings in question I have
> disabled that until I can get it to read something from the stream.
>
> I have tried to use readLine() but it returns to me an empty string. I
> have tried the following to input a line but it does not work. I inserted a
> messagebox pop up that confirms that nothing is being read from the stream
> when I use
>
> 1.) str = input.readLine(); or
> 2.) str = QString::fromLocal8Bit(file.readLine().data());
>
>
> void MainWindow::load()
> {
> QString str, chopped;
> int num;
>
> QFile file("settings.cfg ");
> if (!file.open(QIODevice::ReadOnly))
> {
> cerr << "Cannot read file for loading: " << qPrintable(file.errorString())
> << endl;
> loadDefaults();
> return;
> }
> QTextStream input(&file);
> // Server
> input >> str;
> chopped = str.section('=', 1);
> server_lineEdit->setText(chopped);
>
> // Protocol
> input >> str;
> chopped = str.section('=', 1);
> if ( chopped == "sftp" )
> sftp_radioButton->setChecked(true);
> else
> ftp_radioButton->setChecked(true);
>
> // Port
> input >> str;
> chopped = str.section('=', 1);
> num = chopped.toInt();
> port_spinBox->setValue(num);
> // Username
> input >> str;
> chopped = str.section('=', 1);
> username_lineEdit->setText(chopped);
>
> // Password
> input >> str;
> chopped = str.section('=', 1);
> password_lineEdit->setText(chopped);
>
> // Backup Directory
> //input >> str;
> str = input.readLine();
> //str = QString::fromLocal8Bit( file.readLine().data());
> //chopped = line.section('=', 1);
>
> QMessageBox::information(this, tr("STR"),
> QString("Backup Directory String is: %1 ").arg(str) );
>
> chopped = str.mid (str.indexOf('=')+1);
> backup_dir_lineEdit->setText(chopped);
>
> // Firefox
> input >> str;
> chopped = str.section('=', 1);
> if (chopped == QString::number(1))
> firefoxBox->setChecked(false);
> else
> firefoxBox->setChecked(true);
> // Path
> //input >> str;
> str = input.readLine();
> chopped = str.section('=', 1);
> firefox_path_lineEdit->setText(chopped);
> }
>
>
> Any insight would be greatly appreciated. I have no idea what else to try
> or what could be causing this. Any other alternatives?
>
>
>