QTextStream can't separate lines by '\n' char
-
Hi,
I'm using QTextStream to read textual data line by line in QList<QString>. Here is the code:
ReadTraceHeaderNames(){ QFile qFile(":/MyFiles/additional/TraceHeaderNames.txt"); if (!qFile.open(QFile::ReadOnly | QFile::Text)){ QMessageBox::critical(nullptr, "Error", "Cannot open TraceHeaderNames.txt file!"); return; } QTextStream in(&qFile); int k = 0; while (!in.atEnd()) { traceHeaderNames.push_back(in.readLine()); } qFile.close(); }But while loop workes only ones and reads the whole file in the
traceHeaderNames. Lines are delimited by\nchar (you can see this in the picture). Are lines supposed to be ended with\0or other char?

-
Hi,
I'm using QTextStream to read textual data line by line in QList<QString>. Here is the code:
ReadTraceHeaderNames(){ QFile qFile(":/MyFiles/additional/TraceHeaderNames.txt"); if (!qFile.open(QFile::ReadOnly | QFile::Text)){ QMessageBox::critical(nullptr, "Error", "Cannot open TraceHeaderNames.txt file!"); return; } QTextStream in(&qFile); int k = 0; while (!in.atEnd()) { traceHeaderNames.push_back(in.readLine()); } qFile.close(); }But while loop workes only ones and reads the whole file in the
traceHeaderNames. Lines are delimited by\nchar (you can see this in the picture). Are lines supposed to be ended with\0or other char?

are you on Windows? Then text files have to end lines with
"\r\n".'\n'alone is Unix format.Regards
-
are you on Windows? Then text files have to end lines with
"\r\n".'\n'alone is Unix format.Regards
@aha_1980 Yes i'm on Windows 10
I just tried to change end of line character from'\r\nto\nbut stilltraceHeaderNames.push_back(in.readLine())reads the whole file.
traceHeaderNames.push_back(in.readLine(10))also reads the whole file. Can't understand how to read only one line? -
@aha_1980 Yes i'm on Windows 10
I just tried to change end of line character from'\r\nto\nbut stilltraceHeaderNames.push_back(in.readLine())reads the whole file.
traceHeaderNames.push_back(in.readLine(10))also reads the whole file. Can't understand how to read only one line?@Please_Help_me_D said in QTextStream can't separate lines by '\n' char:
I just tried to change end of line character from '\r\n to \n
Why? On Windows \r\n is used, not \n as @aha_1980 wrote.
-
@Please_Help_me_D said in QTextStream can't separate lines by '\n' char:
I just tried to change end of line character from '\r\n to \n
Why? On Windows \r\n is used, not \n as @aha_1980 wrote.
@jsulm I tried both end of line character but they don't work anyway.
I add my text file via Qt resources. There should be a reason why I can't read my file line by line ... -
Hi,
I'm using QTextStream to read textual data line by line in QList<QString>. Here is the code:
ReadTraceHeaderNames(){ QFile qFile(":/MyFiles/additional/TraceHeaderNames.txt"); if (!qFile.open(QFile::ReadOnly | QFile::Text)){ QMessageBox::critical(nullptr, "Error", "Cannot open TraceHeaderNames.txt file!"); return; } QTextStream in(&qFile); int k = 0; while (!in.atEnd()) { traceHeaderNames.push_back(in.readLine()); } qFile.close(); }But while loop workes only ones and reads the whole file in the
traceHeaderNames. Lines are delimited by\nchar (you can see this in the picture). Are lines supposed to be ended with\0or other char?

@Please_Help_me_D I had this, or something similar in the past. Think it was a lifetime issue for me.
I'm not 100% sure why as the returned QString refcounted should be increased, but it happens.So, do not inline the readLine().
Store your readLine() result in a local (temporary) QString variable andpush_backthat one. Should work.while (!in.atEnd()) { QString line = in.readLine(); traceHeaderNames.push_back(line); } -
@Please_Help_me_D I had this, or something similar in the past. Think it was a lifetime issue for me.
I'm not 100% sure why as the returned QString refcounted should be increased, but it happens.So, do not inline the readLine().
Store your readLine() result in a local (temporary) QString variable andpush_backthat one. Should work.while (!in.atEnd()) { QString line = in.readLine(); traceHeaderNames.push_back(line); }@J-Hilk I just tried but this doesn't work neither :)
Moreover there is noQString linevariable in debug at all.
I use this code in the namespace (.header file):class ReadTraceHeaderNames { public: ReadTraceHeaderNames(){ QFile qFile(":/MyFiles/additional/TraceHeaderNames.txt"); if (!qFile.open(QFile::ReadOnly | QFile::Text)){ QMessageBox::critical(nullptr, "Error", "Cannot open TraceHeaderNames.txt file!"); return; } QTextStream in(&qFile); QList<QString> traceHeaderNames; while (!in.atEnd()){ QString line = in.readLine(); traceHeaderNames.push_back(line); } qFile.close(); } };I added textual file via Qt Resources. I attach this textual file to the post here is the link

-
I think I found a problem!
Before now I used to call ReadTraceHeaderNames by typing:util::ReadTraceHeaderNames();But after I assigned this to a variable:
util::ReadTraceHeaderNames a = util::ReadTraceHeaderNames();everithing works fine! I can read line-by-line my file and I can see all the variables in debug window.
So thank you for participating in discussion. Does anybody know why this works only when I assign class to a variable?
-
I think I found a problem!
Before now I used to call ReadTraceHeaderNames by typing:util::ReadTraceHeaderNames();But after I assigned this to a variable:
util::ReadTraceHeaderNames a = util::ReadTraceHeaderNames();everithing works fine! I can read line-by-line my file and I can see all the variables in debug window.
So thank you for participating in discussion. Does anybody know why this works only when I assign class to a variable?
@Please_Help_me_D said in QTextStream can't separate lines by '\n' char:
util::ReadTraceHeaderNames();
because this creates a new instance each time you call it
-
Just as a note.
Your file was fine :)
-
@mrjj yes, thank you
I've just found a way to show end of file character in notepad :)