Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Parsing a text file written with qSetFieldWidth



  • Hello, I wrote a text file using qSetFieldWidth to format the columns (different columns have different widths). The last column was reserved for special notes, which can contain spaces. Is there a way to parse the file using QTextStream and split each line based on the fieldWidth?

    For example:
    The text file could look like this:

    @ ID StartTime EndTime GoodExample Problem Complexity Priority Note
    1 17:27:46:778 08:34:11:815 no None NA NA S
    2 08:34:11:816 08:34:11:946 no None NA NA T
    3 08:34:17:437 08:34:17:570 no None NA NA G
    4 08:34:17:437 08:34:17:570 no None NA NA G
    5 08:34:23:425 08:34:23:557 no None NA NA G
    6 08:36:50:114 08:36:58:049 no None NA NA C, T, G @
    Note: start of each line is the ID (1, 2, 3, etc.), but I couldn't find a good way to format it here.

    I would like the string list of the sixth line that looks like this:

    • 6
    • 17:27:46:778
    • 08:34:11:815
    • no
    • None
    • NA
    • NA
    • C, T, G

    Is there a simple(ish) way to do this? Thanks.


  • Lifetime Qt Champion

    Hi,

    You could try with QString::split and a QRegularExpression like \s

    Hope it helps



  • Thanks! That's basically what I ended up doing, I was just hoping there was something symmetric to qSetFieldWidth. Your solution is certainly effective!


Log in to reply