Write into file



  • Hi,

    Is there any possibility to write QStringlist or QQueue into text file without iteration? I'm looking for a solution like SaveToFile in delphi.

    Any clue?

    Gabor



  • If you're not in need of a human readable/editable file, I would recommend [[Doc:QDataStream]] together with "operator<<".



  • Thanks for your quick reply.

    The output file should look like this:
    MSH|^~&|LABOR||MEDIKA||201101010032||ORR^O02|7500421|P|2.3|||NE|AL||
    MSA|AA|2714415|
    ORC|OK|7500421^MEDIKA|7500421^LABOR||IP||^^^^||||||||||

    I write the file under win and is reading under unix. Carriage return closes the lines. According the Qt Assistant no input parameter like QString or QQueue at “operator<<”. I guess I can't get rid of the iteration:(



  • No, of course you need a bit of custom parsing to handle that. QTextStream and QString::split may come in handy.



  • You did not answer the question regarding human readability.

    The questions are:

    must the resulting file be human readable and/or editable

    is the file written using Qt only

    is the file reaad using Qt only

    Answer all of these with yes or no.

    The result will influence the way to store and retrieve the data.



  • Sorry,

    The answers:

    1. yes, it must be editable by vi or nano
    2. yes, under windows
    3. no, reading is under unix


  • [quote author="arabiata" date="1312982497"]Sorry,

    The answers:

    1. yes, it must be editable by vi or nano
    2. yes, under windows
    3. no, reading is under unix[/quote]

    On Unix you can have Qt too :-)

    Anyways, as the blocker is the "editable by...", I would go like this:

    @
    QStringList sl;
    // fill it somewhere

    QFile file("output.txt");
    if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
    // error processing here
    break;
    }

    QTextStream ts(&file);
    ts << sl.join('|');
    ts << "\r"; // or "\n";
    file.close();
    @

    For the QQueue there is no join() method, you will have to iterate through all the elements:

    @
    QQueue<QString> stringQueue;

    QString delim; // emtpy string
    foreach(const QString &e, stringQueue) {
    ts << delim << e;
    delim = "|";
    }
    ts << "\r"; // or "\n"
    @



  • Volker for president:).
    Thanks



  • [quote author="arabiata" date="1313005537"]Volker for president:).[/quote]

    He would make all of us drive a motorcycle while eating muffins and ban rain in general. I'm voting yes!



  • [quote author="mariusg" date="1313049650"][quote author="arabiata" date="1313005537"]Volker for president:).[/quote]

    He would make all of us drive a motorcycle while eating muffins and ban rain in general. I'm voting yes![/quote]
    I'm voting yes too :-D! In Moscow right now is rain, so ban not prevent.


  • Moderators

    And thus, the Muffincycle Party was born with the nomination of it's first candidate. :-)


Log in to reply
 

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