Text output.



  • Hi,

    I am using Qt Creator 2.0.1 based on Qt 4.7.0 (32 bit); built on Aug 24 2010 at 11:00:55 from revision 97d831e3de.
    The following piece of source code:

    @ QFile data ("iotest.txt");
    if (data.open(QFile::WriteOnly | QFile::Truncate)) {
    QTextStream out(&data);
    out << "Test1:" << endl;
    out << "Test2\r" << endl;
    out << "Test3" << qSetFieldWidth(5) << 1234 << "\r\n";
    data.close();
    }
    @
    produces the output to the text file:

    Test1:Test2
    Test3 1234

    It seems to me that endl does not work properly. Can somebody from technical support comment this.

    Best regards,
    Grzegorz



  • Depends on what program you use to open the output file, with Notepad++ or Wordpad you will have:
    Test1:
    Test2
    Test3 1234

    But if you open with Notepad then you always need the windows version "\r\n" for a new line

    LE: also you can use the QIODevice::Text flag to cause the end-of-line conversion from "\n" to "\r\n" on Windows



  • Hi,

    In my opinion the compiler should recognise the operating system the executable is generated for and should behave respectively. That’s why I asked technical support for comment.

    Proposed solution is kind of amateur and widely known to experienced programmers.

    Best regards,
    Grzegorz



  • I apologize for my amateur solution

    If the Troll's solution (QIODevice::Text) for this isn't good enough and you want a real solution i suggest you contact Gcc and/or Visual Studio teams, since they are making the C++ compilers, or contact Microsoft and suggest to change the way their operating system is behaving in this problem.



  • [quote author="Grzegorz Szewczyk" date="1294609110"]Hi,

    In my opinion the compiler should recognise the operating system the executable is generated for and should behave respectively. That’s why I asked technical support for comment.

    Proposed solution is kind of amateur and widely known to experienced programmers.
    [/quote]

    It's not amateur at all. That's what endl does. http://doc.trolltech.com/4.7/qtextstream.html#endl



  • The idea is to allow you to fine tune how the file is created regardless of the platform so you can use the same code to open "the same file" regardless of the platform. For cross platform applications this is a MUST.



  • [quote author="Grzegorz Szewczyk" date="1294606854"]
    ...
    It seems to me that endl does not work properly. Can somebody from technical support comment this.
    [/quote]

    Hi Grzegorz,

    This is a community forum, not a Qt support forum. Some of the Trolls hang around here, but not for official support.

    And as peppe said, it does exactly what is described, it writes a \n to the file. If the editor of your choice needs \r\n, you have to set the QIODevice::Text flag, and it should work.



  • [quote author="Zlatomir" date="1294609782"]I apologize for my amateur solution [/quote]
    I love the smell of cynism in the morning! :-)



  • @Andre: that is not the case, i really am an amateur programmer and i wouldn't have bothered him with my advices if i knew he already knows the solution ;)



  • [quote author="Zlatomir" date="1294654121"]@Andre: that is not the case, i really am an amateur programmer and i wouldn't have bothered him with my advices if i knew he already knows the solution ;)[/quote]

    Amateur or not I think Grzegorz's answer should be nicer - let's hope it was not intentional. This is a community forum and anybody answering questions is trying to help by providing opinions and/or suggestions - no guarantees.

    Also, when answering a question we don't have any way of knowing the author's level of expertise and, most of the time, have to start by assuming they can be missing something obvious - I don't see any problem with Zlatomir's answer.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.