Pointers and opening a file from argument



  • So, I'm back after a long break of not doing any coding, and I really want to get over with this part: parsing Xml. I belive I've setup a good bit of the parsing part, but I'm lost with how to make pointers and implementing use of command line arguments. (Linux)

    First, pointers. I've searched the web, but I can't find a reasonable explanation as to how I use them to preserve an object across classes or in memory for an extended period of time. I'm hoping that someone will be able to help with that, as I'm going to need it.

    Secondly I'm having a bit problems with receiving command line arguments. In my program, I implemented a function that fetches command line arguments, makes a QStringList and then fetches a QString from that to use as filename for a QFile.
    @ QFile xmlFile;
    QStringList arglist = QCoreApplication::arguments();
    QString args = arglist[0];
    xmlFile.setFileName(args);@
    The above is what I wrote for handling the arguments.
    The below is a snippet from using the QFile in a QXmlStreamReader:
    @
    //Checking if the files is readable and is text
    if (!xmlFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
    return;
    }
    QXmlStreamReader xml(&xmlFile);
    while(!xml.atEnd()) {
    QXmlStreamReader::TokenType token = xml.readNext();
    if (token == QXmlStreamReader::StartElement) {
    if(xml.name() == "zgame") {
    continue;
    }
    if (xml.name() == "title") {
    xml.tokenString().append(gameName);
    }@
    However, when running the app, I get the error:
    @QFSFileEngine::open: No file name specified@
    I don't know what's causing it, and compiler doesn't report anything.
    I guess I did something wrong with the setFileName part..?



  • Your issue is most likely that the first argument in the argument list is the executable file name (and path) so the "first" argument that you write when you execute your application will be arglist [ 1 ] (second argument from the list)

    And for xml parsing i really recommend "QDomDocument":http://doc.qt.nokia.com/latest/qdomdocument.html#details, it-s much easier to use than QXmlStreamReader (just my opinion ;) )



  • The StringList works now, I can confirm that. It seems like the error was in another class. (I sound like Toad saying: "The Princess is in another castle")
    However I've found that the error has happened in the parsing process...
    No string is appended to the QString. What an amusing thing to know.
    I found this out with the qDebug function. (This is a bit like when I script with Bash, only that it's a notch higher.)
    But I can figure that out by myself. :)

    I'm using the QXmlStreamReader in a somehow special way. It's used to fetch a file path which in turn will be used to make a QDomDocument. ;)
    I just wanted to keep the memory usage low, and to put them into QStrings right away is reasonable.
    I don't know if these work yet, because I haven't had any input for it.

    However, I have a hard time understanding a part of objects. When I define an object outside a class and then define something for that object inside a class, like a string, is that information preserved?
    And if it's not, how would I make a pointer to that information to be used in another class?


Log in to reply
 

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