PDA

View Full Version : Reading text file line part by part



anh5kor
22nd December 2015, 10:00
I am trying to Read a text file with the below code


QFile infile("Rte.txt");

if(infile.open(QIODevice::ReadOnly | QIODevice::Text))
{
QString line;
char line1;
QTextStream stream(&infile);
// while(!stream.atEnd())
for(int i = 0;i<20;i++)
{
line = stream.readLine();

}
infile.close();

}

The above code is reading the file Line by Line.Which is correct according to implementation but I want in some other way.
For Example
My txt file look like below pattern


FUNC(uint8, RTE_CODE) Rte_Mode_BswM_RP_GM_DcmControlDtcSetting_DcmContro lDtcSetting(void);
FUNC(uint8, RTE_CODE) Rte_Mode_BswM_RP_GM_DcmEcuReset_Dcm_EcuReset(void) ;
FUNC(Std_ReturnType, RTE_CODE) Rte_Read_BswM_RP_GM_SUM_ECUOperatingMode_RequestPo rt_SUM_ECUOperatingMode(CONSTP2VAR(UInt8, AUTOMATIC, RTE_APPL_DATA) data);
FUNC(Std_ReturnType, RTE_CODE) Rte_Read_BswM_RP_GM_SUM_RxPduMode_14_RequestPort_S UM_PduMode(CONSTP2VAR(UInt8, AUTOMATIC, RTE_APPL_DATA) data);
FUNC(Std_ReturnType, RTE_CODE) Rte_Read_BswM_RP_GM_SUM_RxPduMode_15_RequestPort_S UM_PduMode(CONSTP2VAR(UInt8, AUTOMATIC, RTE_APPL_DATA) data);
FUNC(Std_ReturnType, RTE_CODE) Rte_Read_BswM_RP_GM_SUM_RxPduMode_5_RequestPort_SU M_PduMode(CONSTP2VAR(UInt8, AUTOMATIC, RTE_APPL_DATA) data);
FUNC(Std_ReturnType, RTE_CODE) Rte_Read_BswM_RP_GM_SUM_RxPduMode_6_RequestPort_SU M_PduMode(CONSTP2VAR(UInt8, AUTOMATIC, RTE_APPL_DATA) data);
FUNC(Std_ReturnType, RTE_CODE) Rte_Read_BswM_RP_GM_SUM_RxPduMode_7_RequestPort_SU M_PduMode(CONSTP2VAR(UInt8, AUTOMATIC, RTE_APPL_DATA) data);
FUNC(Std_ReturnType, RTE_CODE) Rte_Read_BswM_RP_GM_SUM_TxPduMode_14_RequestPort_S UM_PduMode(CONSTP2VAR(UInt8, AUTOMATIC, RTE_APPL_DATA) data);
FUNC(Std_ReturnType, RTE_CODE) Rte_Read_BswM_RP_GM_SUM_TxPduMode_15_RequestPort_S UM_PduMode(CONSTP2VAR(UInt8, AUTOMATIC, RTE_APPL_DATA) data);
FUNC(Std_ReturnType, RTE_CODE) Rte_Read_BswM_RP_GM_SUM_TxPduMode_5_RequestPort_SU M_PduMode(CONSTP2VAR(UInt8, AUTOMATIC, RTE_APPL_DATA) data);
........
........

On running the above code its reads line by line

o/p:-FUNC(Std_ReturnType, RTE_CODE) Rte_Read_BswM_RP_GM_SUM_ECUOperatingMode_RequestPo rt_SUM_ECUOperatingMode(CONSTP2VAR(UInt8, AUTOMATIC, RTE_APPL_DATA) data);(for example)

but my requirement is that I should read a line in 4 parts and save the same in other file.

My output should be like below
1.o/p:-FUNC(Std_ReturnType, RTE_CODE)
2.o/p:- Rte_Read_BswM_RP_GM_SUM_ECUOperatingMode_RequestPo rt_SUM_ECUOperatingMode
3.o/p:-UInt8, AUTOMATIC, RTE_APPL_DATA
4.o/p:-data

please let me know how can I read a single line part by part.

anda_skoa
22nd December 2015, 10:24
You just read line-by-line and then parse/split the line according to you criteria.

Cheers,
_

anh5kor
22nd December 2015, 11:48
You just read line-by-line and then parse/split the line according to you criteria.

Cheers,
_

I used the below split function


list = line.split(QRegularExpression("\\W+"), QString::SkipEmptyParts);


And its working Perfectly for me.Thanks for the information.

anh5kor
23rd December 2015, 10:08
According to my new Requirement I have to spilt the below line as
For example


FUNC(Std_ReturnType, RTE_CODE) Rte_Read_PA_BSW_comEBCM_CAN1_PDU82_IElecPrkBrkAppl Stat(CONSTP2VAR(IElecPrkBrkApplStat, AUTOMATIC, RTE_APPL_DATA) data)

My result Should as below
1.FUNC
2.Std_ReturnType, RTE_CODE
3.Rte_Read_PA_BSW_comEBCM_CAN1_PDU82_IElecPrkBrkAp plStat
4.CONSTP2VAR(IElecPrkBrkApplStat, AUTOMATIC, RTE_APPL_DATA) data


I tried many way to spilt but not got the exact Result
Example


list = line.split(QRegularExpression("\\W+"), QString::SkipEmptyParts);

list = line.split("(");

list = line.split(")");

list = line.split("()");

list = line.split(" ");

but all does not provide me the exact Result Please let me know if any idea.

anda_skoa
23rd December 2015, 11:31
QString::split() alone might not be enough or not applicable at all.

If the pattern of the whole line is describable as a regular expression you could use QRegularExpression to match and capture the parts.
If the pattern is not describable as a regular expression, you can check for the tokens manually and then use sub string functions such as mid() to extract the parts.

Cheers,
_