Hi There,

I need to re-write any mention of .tellg() in my code as the function is not supported on the desired hardware. I have the following lines of code:

getline(file,line);
{
    std::istringstream stream(line);
    stream >> first >> second >> third >> forth;
    UINT32 file_pos = stream.tellg();
    file_pos++;
    fifth = line.substr( file_pos , line.length() - stream.tellg() );
}

As far as I can work out, tellg is used to find out where in the line the stream is, so fifth is anything between forth and the end of line.

file_pos++; is to allow for a space.

My questions are:
Is there any easy alternative of finding the position in the stream without using tellg();?
Would I have to get rid of istringstream and think of another way?

Many Thanks in advance

Alex

I find it odd that the implementation supports streams yet not completely. If the hardware couldn't handle stream behavior (presumably this is an embedded platform) then excluding that part of the library entirely would be expected. Rather, I don't recall any clauses in the ISO standard that say tellg() may be conditionally supported in the presence of an otherwise working iostream library, even for freestanding implementations.

But to the matter at hand, istringstream is really doing nothing more here than breaking the line down into fields and converting those fields. Since you already know that the >> operator is whitespace delimited, you can get the same value as stream.tellg() by counting delimiting whitespace for the first four fields:

getline(file,line);
{
    std::istringstream stream(line);
    UINT32 file_pos, fields;

    stream >> first >> second >> third >> forth;

    for (file_pos = 0, fields = 0; file_pos != line.size(); ++file_pos) {
        // Assuming fields are separated by only a single delimiter
        // and the line doesn't begin with whitespace. These are
        // fairly easy to handle, but I'll leave it to you.
        if (isspace(line[file_pos])) {
            if (++fields == 4)
                break;
        }
    }

    fifth = line.substr(++file_pos , line.length() - stream.tellg());
}

tellg() in my code as the function is not supported on the desired hardware.

What hardware? A std::istringstream holds a std::stringbuf and it does not use anything other than memory allocated with the standard allocator.

Have you tried printing out the result returned by tellg() on the string stream?

Thank you very much for your responces. I think its something to do with a certain version of the GNU GCC complier and using MINGW. Works fine in Windows but not in VxWorks:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13333

Anywho, thanks deceptikon for your code and your idea about using the whitespace as a counter.

Very Hepfull indeed.

Thanks

I think its something to do with a certain version of the GNU GCC complier and using MINGW. Works fine in Windows but not in VxWorks:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13333

"fstream tell/seek bug with DOS-format text files"

seekg() and tellg() on fstream (not stringstream).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.