Discussion Board

Results 1 to 14 of 14
  1. #1
    Regular Contributor ginda1's Avatar
    Join Date
    Nov 2010
    Posts
    52
    Hi all

    I am currentley reading in a line of text from a text file using the below code:

    Code:
        QFile settingsfile("file.txt");
        settingsfile.open(QIODevice::ReadOnly | QIODevice::Text);
        QTextStream in(&settingsfile);
        QString line = in.readLine();
        return line;
    i know that the readLine() reads in a line of text, but is it possible to read in lines sequencially? or readling in certain lines i.e. line 1, line2 etc

    file.txt contains
    Code:
    10
    5
    black
    blue

  2. #2
    Registered User DianaM's Avatar
    Join Date
    Dec 2010
    Posts
    9
    You can call readLine() more than once.

  3. #3
    Regular Contributor ginda1's Avatar
    Join Date
    Nov 2010
    Posts
    52
    Quote Originally Posted by DianaM View Post
    You can call readLine() more than once.
    I was hoping i wouldnt have to loop to a line instead read a line, by its line number. Never mind. Thanks for your reply.

  4. #4
    Nokia Developer Moderator achipa's Avatar
    Join Date
    Oct 2009
    Location
    Finland
    Posts
    327
    Quote Originally Posted by ginda1 View Post
    I was hoping i wouldnt have to loop to a line instead read a line, by its line number. Never mind. Thanks for your reply.
    Plain text files are generally not indexed structures, so you would need to read in all previous lines anyway. If your file/lines are really huge, you might want to have a sort of index in the first line (like relative byte positions separated in CSV form or sorts).

  5. #5
    Nokia Developer Champion danhicksbyron's Avatar
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209
    If you have fixed-length records you can alway use seek, but I know of no standard programming environment that will give you records by number otherwise.

  6. #6
    Registered User Jon Heron's Avatar
    Join Date
    Feb 2010
    Posts
    210
    Look into QString::section().
    I was able to use it to pick out desired sentences from one big text file to display the desired directions along with the images that can be iterated through (for a knot tying app).
    Here is what I used to get it to work, maybe it will do what you need?
    Code:
        //open file for instructions
        str = (QString::fromUtf8(":/images/docs/texts/instructions/Instructions.txt"));
        QFile inputFile2(str);
        inputFile2.open(QIODevice::ReadOnly);
        QTextStream in2(&inputFile2);
        QString line2 = in2.readAll();
        QString instructions;
        inputFile2.close();
        str1.remove(" ");
        instructions = line2.section(str1, 1, 1); //load instructions with all the selected instructions
        int tmp(strLst.count()); //setup counter with number of images
        for(int i = 0; i < tmp; i++)
        {
            inst += instructions.section('.', i, i);  //load strlist inst with each sentance
         //  qDebug() << "*****" << inst.at(i) << "*******";
        }
    Good luck!
    Jon

  7. #7
    Nokia Developer Champion danhicksbyron's Avatar
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209
    Generally, if you have a file full of options entries or some such the best way to handle it is to use XML or JavaScript. But this means you read the entire file up front.

    If you really want to do random access, and you don't have fixed-length records, you need some sort of directory. Easy for me to do, but probably not the OP. For the OP's problem, if it involves more than a handful of values, a SQL database may be the best approach.

  8. #8
    Registered User Jon Heron's Avatar
    Join Date
    Feb 2010
    Posts
    210
    Thanks!
    When you say
    If you really want to do random access, and you don't have fixed-length records, you need some sort of directory.
    Are you referring to a key/map type of setup?
    For someone like me who doesn't know Java, XML or anything about data bases, which direction would you encourage them to go?
    I have been thinking about another app that that would have many more instructions to weed through. Though the code I posted earlier did work I had to make a few switch statements to filter through knots with duplicate names. Not a big deal in an app with 27 knots but I think it would become cumbersome if there were say 100 knots...
    Thanks again,
    Jon

  9. #9
    Nokia Developer Champion danhicksbyron's Avatar
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209
    Well, you're going to have to actually learn SOMETHING. JavaScript (not Java) is simple, but there's no built-in support, so you have to find one of the available libraries and add it to your app. XML is more complicated, but built-in. For both you don't get true random access -- you read through the entire file (or until you find what you want).

    SQL is more general -- you can do true random access and find things with all sorts of search criteria -- but there's a learning curve (though probably no worse than XML to get started). So it's probably the best way to go unless you have some specific reasons for using some other scheme.

    For SQL I'd recommend first getting a GUI SQL app for your PC such as "SQLite Expert" (SQLite is the version of SQL supported on Symbian) and playing around with it, perhaps in combo with a basic book on SQL (or maybe you can find a good online tutorial). Only after you understand what a table is, how to create a query, etc, should you attempt any Qt coding, and then it would be best to start with desktop Qt.

  10. #10
    Registered User Jon Heron's Avatar
    Join Date
    Feb 2010
    Posts
    210
    Thanks.
    Everything has a learning curve for this guy! lol
    Only after you understand what a table is, how to create a query, etc, should you attempt any Qt coding, and then it would be best to start with desktop Qt.
    I believe I understand what your talking about, years ago I used to play around with assembly language where I had to use key/map type look up tables for certain functions on the TMS370 processor. It wasn't too bad in assembly, once you understood what you were trying to do... I would imagine it would be easier in Qt but what the hell do I know!
    In any event, I will look into these options when the time arrives!
    Thanks for your input!
    Cheers,
    Jon

  11. #11
    Registered User miksuh's Avatar
    Join Date
    Nov 2007
    Location
    Espoo, Finland
    Posts
    25
    This is not really answer to the original question, but I see you are trying to read some settings file. Are those settings stored by your application or is that some external settings file coming from somewhere else?

    I just mean that it would be much easier to store and read application specific settings using QSettings class. If you use QSettings then you don't need to handle settings file writing and reading by yourself.

  12. #12
    Nokia Developer Champion danhicksbyron's Avatar
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209
    Just realized I referred to "JavaScript" below when I meant "JSON".

  13. #13
    Nokia Developer Champion danhicksbyron's Avatar
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209
    I have found desktop Qt to be a good tool for "fooling around" with SQLite -- trying various techniques.

  14. #14
    Regular Contributor ginda1's Avatar
    Join Date
    Nov 2010
    Posts
    52
    Hi sorry for delayed reply. Thanks to everyone who replied. I achieved what i wanted by reading each line into a qlist and accessing the qlist by at index.

Similar Threads

  1. Replies: 5
    Last Post: 2008-09-23, 09:39
  2. How to read text file line by line?
    By aluzi in forum Symbian C++
    Replies: 12
    Last Post: 2008-03-25, 13:06
  3. Read text Line from text File
    By CarusoAlex in forum Symbian C++
    Replies: 4
    Last Post: 2008-02-06, 12:24
  4. Read text file line by line
    By fdelvalle in forum Symbian C++
    Replies: 5
    Last Post: 2007-01-31, 09:48
  5. Reading text files line by line
    By kdejong in forum Symbian C++
    Replies: 2
    Last Post: 2003-10-25, 23:49

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved