How can I get QIODevice::readLine to ignore newline embedded within quotes?

  • I am trying to parse a CSV file that has fields with embedded newlines ("\n" enclosed within quotes). I want readLine() to ignore those newlines. Is there a way to do that?

    "first field","second field","this is a field\nwith two lines","last field"\n

    Currently readLine() will only return the the first two and a half fields because it stops at the first newline. It would be great if there was a readLine(bool ignoreNewlinesWithinQuotes = false) method. Any ideas on how to make this work with existing code?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Unless your file is huge, you might have better luck reading the whole file and use QRegularExpression to parse it.

    Hope it helps

  • That's my problem. The file is huge and I don't want to read the whole thing into memory. I just want to process it a line at a time.

  • How about counting the quotation marks in the just read line, and if the number is odd, read the next line and append it to the previous line, repeat until the count is even on the appended string.

  • Subclass QFile and create readLine(bool ignoreNewlinesWithinQuotes = false) yourself?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.