PDA

View Full Version : Reading delimited blocks in a txt file and puting it in a list of strings



Momergil
18th July 2011, 16:05
Hello!

I have the need of creating a software that is capable of reading lines from a text (let us say, from a .txt file) in pre-programmed blocks and saving them in a list of strings.

So, for example, here is the first line of the cput-stat file of my PC:


cpu 345652 7067 100651 5176702 20949 2797 1185 0 0


Imagine that I'm trying to read this first line (which is what I'm actually trying, for tests). But I don't want to read the entire line directly; i want to read just the "cpu" part. In this case, I would use the Qt function "readLine(3)" as a way of doing it

But what if I want to read only the second column? [345652] How can I say in a function that I want to read from bit 6 to 12 only and save it in a string without the rest of the line?

If I'm not confusing things, when I began learning C++ in the university my professor show me a function, in C++, that one could use to read a line and stop reading when a empty space appear. If that is correct, than I could use a series of this function in order to read all of the column, save thoose that I want and discard the undesireble ones.

The problem is: I don't know anymore the code for that function.


So my question is: how can I read, ether with C++ or Qt codes, delimited blocks of data in a line of text?


I would be glad if someone could give me a code example for that.


Thanks!

mcosta
18th July 2011, 16:11
IMHO you can:


Read the entire line with QIODevice::readLine
split the line in tokens with QString::split
read the token you need QList::at

high_flyer
18th July 2011, 16:22
This:

than I could use a series of this function in order to read all of the column, save thoose that I want and discard the undesireble ones.
is conflicting with

But I don't want to read the entire line directly; i want to read just the "cpu" part. In this case, I would use the Qt function "readLine(3)" as a way of doing it

So either its ok for you to read the whole line, parse it, and discard what you don't need , or its not ok.
You have to decide.

If I'm not confusing things, when I began learning C++ in the university my professor show me a function, in C++, that one could use to read a line and stop reading when a empty space appear.
Various libs implement readline() function, which usually reads until the first \r\n, including Qt (see QDevice or QFile, QTextStream)


i want to read just the "cpu" part. In this case, I would use the Qt function "readLine(3)" as a way of doing it
This prerequisites that you know the correct starting points of your columns.
If this is the case you can jump directly to a start of a desired "column" by using seek().
Note, you will have to bookkeep your position in the file, as the file is serial, and "columns" are only a logical interpretation done by you.

ChrisW67
18th July 2011, 23:57
There a many ways to achieve this. Here is one that uses QTextStream to worry about finding the white space delimiter:


#include <QtCore>
#include <QDebug>

int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);

QFile in("/tmp/data.txt");
if (in.open(QIODevice::ReadOnly)) {
QTextStream s(&in);
while (!s.atEnd()) {
QString dummy, value;
s >> dummy; // ignore the first field
s >> value;
dummy = s.readLine(); // ignore the remainder of the line
qDebug() << value;
}
in.close();
}
}


No matter what approach you take you have to read the entire line in order to get to the next line.

SixDegrees
19th July 2011, 00:39
The STL function you're thinking of is istream::getline. You use the second argument to change the delimiter from the default newline to whatever separator you want, in this case a space.

To allow for cases where there are multiple spaces between words, you'll also need a function to strip leading/trailing space off the resulting strings. The std::string class provides everything you need to implement this.

Momergil
19th July 2011, 12:31
This:

is conflicting with


So either its ok for you to read the whole line, parse it, and discard what you don't need , or its not ok.
You have to decide.

Hello, hygh_flyer!

First, thanks for the answer. But second, I would like to notice that there is no conflict between the two sentences, since the first one describes the type of function I want (one that can read delimited parts of a line directly or else read part by part, save those that I want and discard those that I don't) and the second, an example =]

But anyway, thanks!

Hello everyone!

I would like to thank you for all of your answers. I will try one by one to see what is the best and I hope that till midday I'll come back here to close the topic saying that the problem is solved :)

Thank you once again!

God bless!