Results 1 to 5 of 5

Thread: Read a file after a specific line

  1. #1
    Join Date
    Feb 2016
    Posts
    7
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Read a file after a specific line

    Hi,
    I want to read a file from a specific line to end of file.

    For example the file contains following text:

    Qt Code:
    1. GLOBAL SYMBOLS: SORTED ALPHABETICALLY BY Name
    2.  
    3. page address name
    4. ---- ------- ----
    5. abs ffffffff .text
    6. 0 0000912a C$$EXIT
    7. 0 000091c5 I$$DIV
    8. 0 000091d6 I$$MOD
    9. ....
    10. ....
    11. abs ffffffff binit
    12. 0 00000122 cinit
    13. 0 00000000 code_start
    14. abs ffffffff etext
    15. abs ffffffff pinit
    16.  
    17.  
    18. GLOBAL SYMBOLS: SORTED BY Symbol Address
    19.  
    20. page address name
    21. ---- ------- ----
    22. 0 00000000 code_start
    23. 0 00000122 ___cinit__
    24. 0 00000122 cinit
    25. 0 00000144 _F28x_usDelay
    26. 0 00000148 _InitGpio
    27. 0 00000188 _GPIO_SetupPinMux
    28. 0 00000214 _GPIO_SetupPinOptions
    29. 0 000002a8 _GPIO_SetupLock
    30. 0 000002d1 _GPIO_SetupXINT1Gpio
    31. 0 000002db _GPIO_SetupXINT2Gpio
    32. 0 000002e5 _GPIO_SetupXINT3Gpio
    33. 0 000002ef _GPIO_SetupXINT4Gpio
    34. 0 000002f9 _GPIO_SetupXINT5Gpio
    35. 0 00000303 _GPIO_EnableUnbondedIOPullupsFor176Pin
    36. 0 0000031c _GPIO_EnableUnbondedIOPullupsFor100Pin
    37. 0 00000341 _GPIO_EnableUnbondedIOPullups
    38. ...
    To copy to clipboard, switch view to plain text mode 

    Questions:

    1) How can I read file from the line "GLOBAL SYMBOLS: SORTED BY Symbol Address" to end of file.
    2) How can I determine which line number in text file that sentence stays?
    3) How can I read file from a specific line to a specific line. For example I want to read all lines only between "GLOBAL SYMBOLS: SORTED ALPHABETICALLY BY Name
    " and "GLOBAL SYMBOLS: SORTED BY Symbol Address"


    Thanks.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read a file after a specific line

    You have to read file line by line and ignore all data before and after this special lines.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Read a file after a specific line

    1) How can I read file from the line "GLOBAL SYMBOLS: SORTED BY Symbol Address" to end of file.
    As Lesiok says, you have to start reading at the first line and read the file line-by-line. Starting at line 1, you look for a line containing your "GLOBAL SYMBOLS" string. Until that point, you just read lines and throw away the information. When you find the matching line, you switch to a different mode where you do something with the information, until you find the other "GLOBAL SYMBOLS" line. Then you can stop reading.

    2) How can I determine which line number in text file that sentence stays?
    Create a variable to hold the line number. Start it at zero. Each time you read a line, add 1 to the value. If you need to remember where specific lines are, create other variables for that, and when you hit those lines, copy the line number variable's value into them.

    3) How can I read file from a specific line to a specific line. For example I want to read all lines only between "GLOBAL SYMBOLS: SORTED ALPHABETICALLY BY Name
    " and "GLOBAL SYMBOLS: SORTED BY Symbol Address"
    See the answer to question 1.

    Qt Code:
    1. // Pseudocode:
    2. QFile file( "MyFile.asm" );
    3. QTextStream textStream( &file );
    4. int lineNumber = -1;
    5. bool bProcessing = false; // Becomes true after the first "GLOBAL..." is matched
    6. QString line;
    7. while ( !textStream.atEnd() )
    8. {
    9. line = textStream.readLine();
    10. lineNumber++;
    11.  
    12. if ( line == "GLOBAL SYMBOLS blah blah blah" ) // first matching case
    13. {
    14. bProcessing = true;
    15. }
    16. else if ( bProcessing )
    17. {
    18. // Do something with the line
    19. }
    20. else if ( line == "GLOBAL SYMBOLS blah de dah" ) // second matching case
    21. {
    22. break; // Done reading
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. The following user says thank you to d_stranz for this useful post:

    kahlenberg (20th September 2018)

  5. #4
    Join Date
    Feb 2016
    Posts
    7
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Read a file after a specific line

    Thank you very much. I just asked the question whether there is a built-in function in Qt that makes all those things. Ok I can do it with your code as well.

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Read a file after a specific line

    Your problem is much too specific for there to be something in any library that would solve it without any modification.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. how can i read a specific string from file?
    By sajastu in forum Qt Programming
    Replies: 5
    Last Post: 16th January 2015, 23:30
  2. Replies: 1
    Last Post: 18th January 2012, 09:28
  3. Read gzip file line-by-line
    By The_Fallen in forum Qt Programming
    Replies: 4
    Last Post: 6th September 2011, 13:41
  4. Start writing from a specific line in a file
    By nackasha in forum Newbie
    Replies: 1
    Last Post: 17th August 2011, 17:31
  5. Read a specific line from a file
    By rleojoseph in forum Qt Programming
    Replies: 11
    Last Post: 21st March 2011, 11:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.