How do I read in a text file line by line that uses Hex0D as a line terminator?
-
wrote on 9 Mar 2011, 14:57 last edited by
I have a number of scripts that appear to use Hex0D as a line terminator (I checked using an on-line hexdump utility).
NOTE: I'm running Qt on Windows.
The following code reads the whole file in one go:
@
QFile file(fileName);
file.open(QIODevice::ReadOnly | QIODevice::Text))
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readline();
(do stuff)
}
@ie it ignores the Hex0D line terminator.
I tried using read() to read character by character. I replaced the last line with
@QString character = in.read(1);@
However this just seems to ignore the Hex0D characters altogether.Thanks for any help.
-
wrote on 9 Mar 2011, 15:07 last edited by
It's a bit odd that you have files delimiting lines with \r, but there you go. Since QTextStream doesn't seem to provide a configuration for this and it seems to have been a problem for years. In this case I'd be reading the contents of the entire file into a QString, and split it.
@QStringList lines = contents.split("\r");@ -
wrote on 9 Mar 2011, 15:15 last edited by
Does is not a solution read the file stream in a nested while, breaking every line when the desired character is done? It's a method taken from the serial interface, but can be useful if the file is very big.
-
wrote on 9 Mar 2011, 15:20 last edited by
I would read char by char.
Maybe TextStream automatically trims \r.Try something like this:
@
QFile file(fileName);
file.open(QIODevice::ReadOnly)
while (!file.atEnd())
{
QByteArray line;
while (!file.atEnd()) {
QByteArray readChar = file.read(1);
if (readChar == "\r") {
break;
} else {
line.append(readChar);
}
}
(do stuff)
}
@WARNING: I didn't test this code.. I'm in a hurry. Hope it helps, cya!
-
wrote on 9 Mar 2011, 15:28 last edited by
Jonathan,
it's a solution similar to one I already adopted in past (that obviously I don't find now to post here...)
Only an advice: take care of that "forever" loop. If you file is only one line (or only one character) without any \r character your loop never exit. Simply add in the forever loop a check: if file.read(1) doesn't returns a character, this means that you have reached the EOF so exit anyway from the forever loop.
-
wrote on 9 Mar 2011, 15:31 last edited by
Using
@ if (readChar == "\r") @
works provided that I also drop QIODevice::Text, ie use
@file.open(QIODevice::ReadOnly)@ -
wrote on 9 Mar 2011, 15:33 last edited by
[quote author="Alicemirror" date="1299684492"]Jonathan,
it's a solution similar to one I already adopted in past (that obviously I don't find now to post here...)
Only an advice: take care of that "forever" loop. If you file is only one line (or only one character) without any \r character your loop never exit. Simply add in the forever loop a check: if file.read(1) doesn't returns a character, this means that you have reached the EOF so exit anyway from the forever loop.[/quote]
You're right, instead of
@forever@
should be
@while (!file.atEnd())@I editted the first post.
-
wrote on 9 Mar 2011, 15:35 last edited by
[quote author="Jonathan" date="1299684717"]Using
@ if (readChar == "\r") @
works provided that I also drop QIODevice::Text, ie use
@file.open(QIODevice::ReadOnly)@[/quote]Indeed, and reading the documentation you'll see that QIODevice::Text was the root of your problem :)
-
wrote on 9 Mar 2011, 17:57 last edited by
Actually, the root of the problem is that Qt doesn't accept \r as a valid EOL.
-
wrote on 9 Mar 2011, 18:09 last edited by
Franzk, with the solution suggested by Ivan it does no matter.
What you do is to read from the file one character at a time until you reach the end. For every character, you check if the value corresponds to what you consider the EOL, then the program close the text line and go ahead.
Indeed, using this method your EOL character can be everything you want: is the program that create lines based on the character that you decide. When the line is complete, I suppose that the source creating a line string set automatically the EOL with the right character. A similar approach is those used to manage linux-encoded text files with windows programs and vice-versa. -
wrote on 9 Mar 2011, 18:11 last edited by
[quote author="Franzk" date="1299693469"]Actually, the root of the problem is that Qt doesn't accept \r as a valid EOL.[/quote]
Why should it? No platform supported by Qt uses that control character. -
wrote on 9 Mar 2011, 18:16 last edited by
Why complicating a simple thing? There are tons of cases where a file is non standard. Reading character by character the problem does not exist.
Or not ?
-
wrote on 9 Mar 2011, 18:17 last edited by
It would be nice to be able to control it though. If you have to produce files that have to be processed by other tools, control is needed.
-
wrote on 9 Mar 2011, 18:19 last edited by
Andre, what does you means with "control"? To set a control like those explained above or what ?
-
wrote on 9 Mar 2011, 18:23 last edited by
[quote author="Alicemirror" date="1299694794"]Andre, what does you means with "control"? To set a control like those explained above or what ?[/quote]
I mean that I, as a programmer, can choose what QTextStream and QIODevice considder the line separator. Sure, you can output your data character by character, but I did not choose Qt to do everything by hand...
-
wrote on 9 Mar 2011, 18:29 last edited by
[quote author="Franzk" date="1299693469"]Actually, the root of the problem is that Qt doesn't accept \r as a valid EOL.[/quote]
Well, yeah.. That's the actual root of the problem :/
I've been reading too much Qt Labs Blog posts.. Sadly, it seems I'm becoming one of those qt-is-perfect-no-need-to-change-anything guys >.<
Jonathan, you should file a bug report http://bugreports.qt.nokia.com/
-
wrote on 9 Mar 2011, 18:32 last edited by
[quote author="Andre" date="1299694989"][quote author="Alicemirror" date="1299694794"]Andre, what does you means with "control"? To set a control like those explained above or what ?[/quote]
I mean that I, as a programmer, can choose what QTextStream and QIODevice considder the line separator. Sure, you can output your data character by character, but I did not choose Qt to do everything by hand...[/quote]
I agree with you. The best solution is indeed that.
QTextStream and QIODevice already have the subroutines to read a file up to some hardcoded characters. It would be wise to add some "control" on what these classes consider a EOL.Then, the same routine could be used to read any character-separated file.
-
wrote on 9 Mar 2011, 18:36 last edited by
[quote author="peppe" date="1299694316"][quote author="Franzk" date="1299693469"]Actually, the root of the problem is that Qt doesn't accept \r as a valid EOL.[/quote]
Why should it? No platform supported by Qt uses that control character. [/quote]Mac is no longer supported?
Even if it is true, \r is still a valid line ending and should therefore be supported, even if only by configuration.
-
wrote on 9 Mar 2011, 18:43 last edited by
[quote author="Franzk" date="1299695807"][quote author="peppe" date="1299694316"][quote author="Franzk" date="1299693469"]Actually, the root of the problem is that Qt doesn't accept \r as a valid EOL.[/quote]
Why should it? No platform supported by Qt uses that control character. [/quote]Mac is no longer supported?
Even if it is true, \r is still a valid line ending and should therefore be supported, even if only by configuration.[/quote]
Just to add to your arguement, a quote from wikipedia:
http://en.wikipedia.org/wiki/Newline :
"Systems based on ASCII or a compatible character set use either LF (Line feed, '\n', 0x0A, 10 in decimal) or CR (Carriage return, '\r', 0x0D, 13 in decimal) individually, or CR followed by LF (CR+LF, '\r\n', 0x0D 0x0A). " -
wrote on 9 Mar 2011, 20:05 last edited by
AH :)
Andre, you are right! As a matter of fact I focused the attention of all my answers thinking that there was a non standard line terminated file, while - silly, real! - \r is the old, famous carriage return...
1/30