Results 1 to 5 of 5

Thread: Reading text file line part by part

  1. #1
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Question Reading text file line part by part

    I am trying to Read a text file with the below code
    Qt Code:
    1. QFile infile("Rte.txt");
    2.  
    3. if(infile.open(QIODevice::ReadOnly | QIODevice::Text))
    4. {
    5. QString line;
    6. char line1;
    7. QTextStream stream(&infile);
    8. // while(!stream.atEnd())
    9. for(int i = 0;i<20;i++)
    10. {
    11. line = stream.readLine();
    12.  
    13. }
    14. infile.close();
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 
    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,396
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading text file line part by part

    You just read line-by-line and then parse/split the line according to you criteria.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    anh5kor (22nd December 2015)

  4. #3
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Thumbs up Re: Reading text file line part by part

    Quote Originally Posted by anda_skoa View Post
    You just read line-by-line and then parse/split the line according to you criteria.

    Cheers,
    _
    I used the below split function
    Qt Code:
    1. list = line.split(QRegularExpression("\\W+"), QString::SkipEmptyParts);
    To copy to clipboard, switch view to plain text mode 

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

  5. #4
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Question Re: Reading text file line part by part

    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
    Qt Code:
    1. list = line.split(QRegularExpression("\\W+"), QString::SkipEmptyParts);
    2.  
    3. list = line.split("(");
    4.  
    5. list = line.split(")");
    6.  
    7. list = line.split("()");
    8.  
    9. list = line.split(" ");
    To copy to clipboard, switch view to plain text mode 
    but all does not provide me the exact Result Please let me know if any idea.

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,396
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading text file line part by part

    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,
    _

Similar Threads

  1. Replies: 2
    Last Post: 23rd August 2011, 12:23
  2. Replies: 3
    Last Post: 8th June 2011, 06:36
  3. Replies: 2
    Last Post: 21st May 2010, 14:32
  4. Replies: 1
    Last Post: 6th November 2009, 10:03
  5. How to make some part of text underlined in QLineEdit?
    By blonde in forum Qt Programming
    Replies: 1
    Last Post: 6th November 2009, 09:43

Tags for this Thread

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.