PDA

View Full Version : Create file and read text with QFile and QTextStream



jano_alex_es
26th May 2009, 14:42
Hi,

in the docs is written how to read text data from a file, how to write text data to a file... but it does not say how to create a file. It always starts with qfile::open but if we don't have that file we need to create it first. How can we do that?

On the other hand, seems like I just need to add "\n" in the middle of the string associated to QString in order to have different rows, is that true? does it work differently in Windows OS?

thanks!

auba
26th May 2009, 16:22
but if we don't have that file we need to create it first. How can we do that?

Doc of QFile::open says:

Note: In WriteOnly or ReadWrite mode, if the relevant file does not already exist, this function will try to create a new file before opening it.



On the other hand, seems like I just need to add "\n" in the middle of the string associated to QString in order to have different rows, is that true? does it work differently in Windows OS?

It works this way: when you only put \n at the end of a line, Notepad on Win32 will not display the text correctly. If you put \r\n at the end, vi will display ^M at the end of each line.
If only QT-Apps use the file, you can use endl and ignore all other editors, else you'll have to replace \r\n or \n to what you want before loading/saving.

fullmetalcoder
26th May 2009, 16:32
It works this way: when you only put \n at the end of a line, Notepad on Win32 will not display the text correctly. If you put \r\n at the end, vi will display ^M at the end of each line.
If only QT-Apps use the file, you can use endl and ignore all other editors, else you'll have to replace \r\n or \n to what you want before loading/saving.
Small corrections :


when opening the device (QFile or any QIODevice) in Text mode, you can just use the LF ('\n') character as it is automatically converted into local line endings (may it be LF, CR or CRLF)
conversely, when reading a file opened in Text mode local line endings are converted into LF to make it easier to process the content without having to mess with line endings
this issue is purely independent of Qt, nowadays most applications are able to read files of any line endings (even Wordpad does it)

jano_alex_es
26th May 2009, 16:59
Doc of QFile:pen says:

My bad! I didn't see that open() overload... I just read the other two :S

thanks!

auba
27th May 2009, 07:26
file opened in Text mode local line endings are converted into LF to make it easier to process the content without having to mess with line endings
Cool, that's new to me too, but very useful. Indeed, I did not care about linefeeds since I'm using qt4, so my info was a little bit old :rolleyes:


nowadays most applications are able to read files of any line endings (even Wordpad does it)

if you mean "nowadays" = Vista... most of our customers are still using XP, some of them even w2k :D

fullmetalcoder
27th May 2009, 12:49
if you mean "nowadays" = Vista... most of our customers are still using XP, some of them even w2k :D
"Nowadays" is not related to OS but to applications. Wordpad from WinME was already able to deal with LF line endings properly (I did not try CR ones but I guess it should have worked as well) and, honestly, apart from Notepad obviously, I have never EVER encountered a worse/more outdated text editor than Wordpad so it acts as a "lowest common denominator" here.