PDA

View Full Version : Read a file after a specific line



kahlenberg
19th September 2018, 13:50
Hi,
I want to read a file from a specific line to end of file.

For example the file contains following text:




GLOBAL SYMBOLS: SORTED ALPHABETICALLY BY Name

page address name
---- ------- ----
abs ffffffff .text
0 0000912a C$$EXIT
0 000091c5 I$$DIV
0 000091d6 I$$MOD
....
....
abs ffffffff binit
0 00000122 cinit
0 00000000 code_start
abs ffffffff etext
abs ffffffff pinit


GLOBAL SYMBOLS: SORTED BY Symbol Address

page address name
---- ------- ----
0 00000000 code_start
0 00000122 ___cinit__
0 00000122 cinit
0 00000144 _F28x_usDelay
0 00000148 _InitGpio
0 00000188 _GPIO_SetupPinMux
0 00000214 _GPIO_SetupPinOptions
0 000002a8 _GPIO_SetupLock
0 000002d1 _GPIO_SetupXINT1Gpio
0 000002db _GPIO_SetupXINT2Gpio
0 000002e5 _GPIO_SetupXINT3Gpio
0 000002ef _GPIO_SetupXINT4Gpio
0 000002f9 _GPIO_SetupXINT5Gpio
0 00000303 _GPIO_EnableUnbondedIOPullupsFor176Pin
0 0000031c _GPIO_EnableUnbondedIOPullupsFor100Pin
0 00000341 _GPIO_EnableUnbondedIOPullups
...


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.

Lesiok
19th September 2018, 13:57
You have to read file line by line and ignore all data before and after this special lines.

d_stranz
19th September 2018, 17:25
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.


// Pseudocode:
QFile file( "MyFile.asm" );
QTextStream textStream( &file );
int lineNumber = -1;
bool bProcessing = false; // Becomes true after the first "GLOBAL..." is matched
QString line;
while ( !textStream.atEnd() )
{
line = textStream.readLine();
lineNumber++;

if ( line == "GLOBAL SYMBOLS blah blah blah" ) // first matching case
{
bProcessing = true;
}
else if ( bProcessing )
{
// Do something with the line
}
else if ( line == "GLOBAL SYMBOLS blah de dah" ) // second matching case
{
break; // Done reading
}
}

kahlenberg
20th September 2018, 10:16
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.

d_stranz
20th September 2018, 19:35
Your problem is much too specific for there to be something in any library that would solve it without any modification.