ifstream and QString
-
I read all lines to vector using ifstream and string:
vector<string> readLines(string filename) { ifstream infile(filename); vector<string> v; string line; bool readedNewline; if (infile.peek() != EOF) while(true) { readedNewline = getline(infile, line).good(); v.push_back(line); if (!readedNewline) break; } return v; }
Now I want replace string by QString; I must change ifstream and getline?
-
but if it's just your own stuff I would use Qt calls instead for everything.
Which Qt class I can use instead of std:ifstream?
And why do you want to store an extra blank item at the end of the vector
This is not required for most cases but sometimes, I want imitate in QString vector text file structure accurate if is possible. I want distinguish between last line without and with \n. If I save this vector to second file , this two files should be identical.
@AndrzejB said in ifstream and QString:
Which Qt class I can use instead of std:ifstream?
QFile file(filename); QVector<QString> vec; if(file.open(QFile::ReadOnly | QFile::Text)) { while(!file.atEnd()) { vec<<file.readLine(); } } else { qDebug()<<file.errorString()<<file.error(); }
-
I read all lines to vector using ifstream and string:
vector<string> readLines(string filename) { ifstream infile(filename); vector<string> v; string line; bool readedNewline; if (infile.peek() != EOF) while(true) { readedNewline = getline(infile, line).good(); v.push_back(line); if (!readedNewline) break; } return v; }
Now I want replace string by QString; I must change ifstream and getline?
@AndrzejB
It's up to you, but why are usingstd::
for everything (I/O, vectors, strings) if you want to interoperate with Qt? If you have a large body of outside code using that I can understand, but if it's just your own stuff I would use Qt calls instead for everything. Else you are wasting time converting betweenstd
& Qt classes.On a separate point, do you not think your code for reading is a bit bloated? And why do you want to store an extra blank item at the end of the vector when you reach end of file? Even if you stick with
std
instead of Qt for file handling, what was wrong with:while (getline(infile, line)) v.push_back(line);
instead of all 7 lines of yours?
-
but if it's just your own stuff I would use Qt calls instead for everything.
Which Qt class I can use instead of std:ifstream?
And why do you want to store an extra blank item at the end of the vector
This is not required for most cases but sometimes, I want imitate in QString vector text file structure accurate if is possible. I want distinguish between last line without and with \n. If I save this vector to second file , this two files should be identical.
-
but if it's just your own stuff I would use Qt calls instead for everything.
Which Qt class I can use instead of std:ifstream?
And why do you want to store an extra blank item at the end of the vector
This is not required for most cases but sometimes, I want imitate in QString vector text file structure accurate if is possible. I want distinguish between last line without and with \n. If I save this vector to second file , this two files should be identical.
@AndrzejB said in ifstream and QString:
Which Qt class I can use instead of std:ifstream?
QFile file(filename); QVector<QString> vec; if(file.open(QFile::ReadOnly | QFile::Text)) { while(!file.atEnd()) { vec<<file.readLine(); } } else { qDebug()<<file.errorString()<<file.error(); }