I have searched for a way before what the most effective way and in this case important the fastest way is to seek backwards in a file to read the very last line in a file.

Like in this case I want to search from the end and backwards to get the whole line in a TextFile.
Important to know is that this file also only can consist of one line.
I am familiar to read a file with StreamReader and the lines can have different lengths.

3 lines in a file:

Abcdefg 1234
Abcdefg 123555
Abcdefg 12344

Recommended Answers

All 12 Replies

Important to know is that this file also only can consist of one line.

3 lines in a file:

Do you people not speak english as a native language... or enjoy confusing the rest of the world.

Here is an important excerpt from, "How to ask questions the smart way"

Write in clear, grammatical, correctly-spelled language
We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding (often enough to bet on, anyway). Answering questions for careless and sloppy thinkers is not rewarding; we'd rather spend our time elsewhere.

So expressing your question clearly and well is important. If you can't be bothered to do that, we can't be bothered to pay attention. Spend the extra effort to polish your language. It doesn't have to be stiff or formal — in fact, hacker culture values informal, slangy and humorous language used with precision. But it has to be precise; there has to be some indication that you're thinking and paying attention.

Spell, punctuate, and capitalize correctly. Don't confuse “its” with “it's”, “loose” with “lose”, or “discrete” with “discreet”. Don't TYPE IN ALL CAPS; this is read as shouting and considered rude. (All-smalls is only slightly less annoying, as it's difficult to read. Alan Cox can get away with it, but you can't.)

More generally, if you write like a semi-literate boob you will very likely be ignored. So don't use instant-messaging shortcuts. Spelling "you" as "u" makes you look like a semi-literate boob to save two entire keystrokes. Worse: writing like a l33t script kiddie hax0r is the absolute kiss of death and guarantees you will receive nothing but stony silence (or, at best, a heaping helping of scorn and sarcasm) in return.

If you are asking questions in a forum that does not use your native language, you will get a limited amount of slack for spelling and grammar errors — but no extra slack at all for laziness (and yes, we can usually spot that difference). Also, unless you know what your respondent's languages are, write in English. Busy hackers tend to simply flush questions in languages they don't understand, and English is the working language of the Internet. By writing in English you minimize your chances that your question will be discarded unread.

Clinton Portis,

If you read carefully again:
"Important to know is that this file also only can consist of one line."

What I meen is that this file also can consist of only one line wich can alter another scenario of approach that I am searching for.
I know that it would be possible to set an offset backwards in a file and start reading from that point. I have done that before.
In that case, I would probably need to read 2 lines instead of only effetively 1 line. At the same time that does not seem to be programatically correct really.

So I am searching for a solution for this.

Thank you

Do you people not speak english as a native language... or enjoy confusing the rest of the world.

Yes, that would be the same as reading the whole file and save the last line in memory.

Thank you but I am still hoping for a programatically correct approach.

http://clipboard.it/v/Wmb/ Works but it not very efficient!

You seem to be interested in extracting only the last line of a text file, without having to read in the entire file.

If this is the case, I will provide a possible way to accomplish this.

You can set the 'get' pointer to the end of the document using seekg()

//Any offset provided as the first argument will be relative to the end of the file
infile.seekg (0, ios::end);

Work your way backwards until at such time you have reached the beginning of a line.. but how do you know when you are at the beginning? Based on your example file, a beginning of a line starts with a capital letter:

int i=0;
char c = '\0';
string sentence;

     seekg(i, ios::end);
     c = infile.peek();
     }while(c >= 'A' && c <= 'Z');

//Extract last line of file
getline(infile, sentence);

The above code is untested, uncompiled, and may contain small easy to fix errors. It is intended as one possible solution out of many possible for a given task.

Line #6 of my code should be:

infile.seekg(i, ios::end);

not necessary: std::ifstream read("file.txt", std::ios_base::ate );
std::ios_base::ate = At The End

i tested it ;)

I like your solution in that it opens the file with the get ponter already set to the end of the file.

I opted to use the the ios::end flag in setg() though because then I can just increment my way from the end of the file until I get to where I want to go.

Perhaps the best solution would have been to combine our efforts; open the file 'at the end' and then seekg( , ios::end) step backwards through the file.

//Also this
}while(c >= 'A' && c <= 'Z');
//should be this
}while(c < 'A' && c > 'Z');

I am happy for your answers, I am trying to follow along in the code.
The problem is that this is native code and not managed code.
I did follow along that you were searching for the first "\n" wich is the way I am looking for.

The second scenario is if it only exists one line in the file, then the "\n" shouldn´t exist as we are getting to the beginning of the file.

As I mentioned before I am looking for a solution like this using StreamReader but are not sure how I will do it.

I know how to use StreamReader when reading files from beginning to end, so I am used to that.

StreamReader^ sr = gcnew StreamReader();

Thank you again.

I think I am searching for a way to read character by character backwards from the end of the file until a "\n" is reached or the beginning of file is reached. From that position I will read the line.

How I normally do is to read a file line by line like this. So I search for a way using StreamReader:

String^ Path = "C:\\File.txt";
FileStream^  sc1 = gcnew FileStream(Path , FileMode::Append, FileAccess::ReadWrite);

StreamReader^ sr = gcnew StreamReader(sc1);

while(sr.Peek() >= 0 )
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.