I have a function below which is supposed to read from a continuous updating file...by another program.
for this reason I used a fileptr int to keep track of the last known good read
so this index value is updating correctly but it is not being used by getline.

and it hangs. How can provide my fileptr index to be used to getline so it can get new lines from that index.

int ReadErrorFile()
{
string line,Strremain;
int found, NP=0xFF;
static long int fileptr=0;
string str2="node id = ";
string dummystrlength ="health:nodeid=";
char temp;

    if(ErrorFile.is_open())
    {
        //cout<<"before seek"<<endl;
        ErrorFile.seekg (fileptr, ios::beg);
        //cout<<"seek end"<<endl;

        while(getline(ErrorFile,line))
        {
                        fileptr= ErrorFile.tellg();
            cout<<"while loop getline"<<line<<fileptr<<endl;
            found=line.find(str2);//gets position of the ID


            //cout<<"after tell"<<line<<endl;

            if  (found!=string::npos)//if line is found get ptr store id for return
            {
                cout<<"FP Value ErrorFile.tellg()"<<fileptr<<endl;
                cout<<line<<endl;
                if(line.find('x')==string::npos) //not a hex value so can read node id value
                {
                        //update the file ptr after every line read
                    stringstream str(line);
                    cout<<"str length"<<dummystrlength.length();
                    for(int i=0; i<dummystrlength.length(); i++)
                    {
                        str >> temp; //getting rid of all chars before the no
                        //cout<<"All chars being rid "<<temp<<endl;
                    }
                        str >> NP;  //read node id
                        cout<<"NP read from the file"<<NP<<endl;
                        break;
                }
            }
        }

    }
    else 
    {
        cout<<" \n Output File is missing \n"<<endl;
    }

When the loop

while(getline(ErrorFile,line))
{
}

exits, the stream is in a failed/eof state. When a stream is in an error state, all operations on the stream will fail. You need to first clear the error state before you do anything more with the stream.

For example, in the following snippet,

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <algorithm>
#include <iomanip>

int main()
{
    std::ifstream file( __FILE__, std::ios::in|std::ios::out) ;
    std::vector< std::streampos > pos ;
    std::string line ;
    pos.push_back( file.tellg() ) ;
    while( std::getline( file, line ) ) pos.push_back( file.tellg() ) ;

    file.clear() ; // clear the failed/eof state of the stream

    std::reverse( pos.begin(), pos.end() ) ;

    {
        // seek, get and print the last line
        if( std::getline( file.seekg( pos[0] ), line ) )
            std::cout << std::setw(5) << pos[0] << ": " << line << '\n' ;
        file.clear() ; // and again clear the failed/eof state of the stream

        // seek, get and print remaining lines (in reverse order)
        for( std::size_t i = 1 ; i < pos.size() ; ++i )
        {
            std::getline( file.seekg( pos[i] ), line ) ;
            std::cout << std::setw(5) << pos[i] << ": " << line << '\n' ;
        }
    }
}
////////////////////////////////////////////////////////////////////////////////

, no output will be generated if you comment out line 16.

As you are trying to read from a file being continuously appended to by another program, it would be prudent to just close the file each time you reach eof. And then reopen it just before the next attempt to read from it.

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.