Hi, i just don't get why this isn't working.
It is supposed to read from the end of a .txt and up, until it hits the first space but
it just keeps repeating the last character in the text file ip.txt
Any help is much appreciated.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    string ip = "a";
    char chr;
    ifstream iptilvar; 
    iptilvar.open ("ip.txt"); //open filestram

    for (int i=-1;ip!=" ";i--) //keep reading until a space is found
    {
        iptilvar.seekg (i, ios::end); //set the getpoint to eof -i
        getline (iptilvar,ip);
        chr = ip[0]; //get the first character of the string called ip
        ip = chr;
        cout<< ip;
    }

    iptilvar.close ();

    return 0;
}

I think the problem is here...

for (int i=-1;ip!=" ";i--)

Since you are using the 'end' flag in seekg(), all offset is relevent to the end of the file.. therefore, you should increment your way from the end of the file, stepping your way backwards into the text file.

You might also have a problem with initializing 'i' to -1.. keep this in mind if you continue to have problems.

Also, I would highly recommend opening your file in binary mode when messing about with the file pointers.. for some reason, when opened in regular ascii mode, it seems to dramatically affect the pointer offset position (you think you are at one place when you are really not)

You have an interesting scheme in place here, getting the line after each character test...

Hmm, good thinking. Doesn't seem to be the problem though.
Also, if I decrease i. like this for (int i=-2;ip!=" ";i--) it reads a character from earlier in the file, so that cant be the problem.

Why not try this:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream iptilvar;
    iptilvar.open ("ip.txt"); //open filestram

    iptilvar.seekg(0, ios::end);
    long size = iptilvar.tellg(); // Get the size of the file
    iptilvar.seekg(0, ios::beg);
    char *buffer = new char[size]; // Make a buffer big enough to hold the file's contents
    iptilvar.read((char*)buffer, size); // Read the contents into the buffer
    int i = size;
    while(buffer[--i] != ' ')
        cout << buffer[i]; // Output the buffer character by character until the first whitespace has been reached, in reverse order
    delete buffer;

    iptilvar.close ();

    return 0;
}

It may not be what you are looking for, but it'll work...

Hope this helped!

@Amrith
When I run that^^
For some reason i get this output:

aDma231.56.512.76

Wich is nowhere to be found in my text file?

EDIT: thats not completely true, the numbers are there, but of course reversed.
But I have no idea where "aDma" comes from

@Amrith
When I run that^^
For some reason i get this output:

aDma231.56.512.76

Wich is nowhere to be found in my text file?

EDIT: thats not completely true, the numbers are there, but of course reversed.
But I have no idea where "aDma" comes from

Could you attach a copy of the file "ip.txt", so that we can have a look at it? Further, how is the file generated, i.e, as another program's output, or written manually with a text editor?

You can also try:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream iptilvar;
    iptilvar.open ("ip.txt"); //open filestream
    // it would be a good idea to add-in some code to check whether the file has actually been opened, here

    string buffer;

    getline(iptilvar, buffer); // read file contents into the string...

    for(unsigned int i = (buffer.length()-1); i >= 0 && buffer[i] != ' '; --i) // Reverse-print buffer contents until the first whitespace...
        cout << buffer[i];

    iptilvar.close ();

    return 0;
}

If you get the same output with this, then its something to do with the file itself...

commented: Thanks for the help in this thread +1

Sure my bad.
The .txt file contains the output of a nslookup, currently it looks like this:

Server:  resolver1.opendns.com
Address:  208.67.222.222

Name:    www.google.dk.opasia.dk
Address:  67.215.65.132

As you have properbly guessed, i need to read the last ip in the file:)

But when i run that code you posted, the output goes completely crazy, it almost looks like Matrix (the movie).

How about something along this line?

nclude <iostream>
#include <fstream>

using namespace std;

int main()
{
   ifstream iptilvar("ip.txt"); //open filestram
   string line, ip;
   while ( getline(iptilvar, line) )
   {
      ip = line;
   }
   string::size_type loc = ip.find_last_of(' ');
   if ( loc != string::npos )
   {
      cout << ip.substr(loc + 1) << "\n";
   }
   return 0;
}

/* my output
67.215.65.132
*/
commented: Would have done the trick perfectly well +1

Thank you for your reply, actually, while you wrote i thought of something similar.
(though i don't understand all of your code)

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    string ip = "a";
    //har chr;
    ifstream iptilvar;
    iptilvar.open ("ip.txt");

    for (int i=1;i!=6;i++)
    {
        getline (iptilvar,ip);
    }
    iptilvar.close ();
    string lars;
    int l;
    l = ip.length();
    ofstream finalout;
    finalout.open ("ip.txt", ios::trunc);
    for (int i=10;i<l;i++)
    {
        finalout<< ip[i];
    }
    finalout.close();

    return 0;
}

And it works:)
So I guess thank you all:)
Problem solved

(I'll rewrite the code since it's quite messy, but this works:) )

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.