I know there is an easy way to do this in C#, but i was wondering about C++. If I want to have an ifstream object defined that will read the input of a file, but i want it to read backwards. So basically here is the format I have.

Order # 1
1 Big Mac Combos.
Sub-Total: $ 5.50
Tax: * $ 0.0875
Total: $ 5.98
==============================================================================

I want it to read backwards and find the keyword "Order", so then i can read the # that I am on so the program can continue with the next number. Can anyone shed some light please? Thank you :) (Not school related, own personal fun)

Recommended Answers

All 4 Replies

Based on your example above, I see no benefit in reading the file in reverse. In my opinion, you could just as well read the file normally and process desired data after "order".

But if you really want to....

I would probably just load the file into a vector and call reverse()... if you really want to read the file literally from back to front, try this.

int pos = 0;
int length = 0;
char c = '\0';
string word;
ifstream infile("file.txt", ios::binary|ios::ate);

length = infile.tellg()-1;
pos = length;

while(length > -1 && word.compare("order"))
{
     infile.seekg(pos-1);

     while(infile.peek() != ' ')
     {
          infile.unget();
     }

     pos = infile.tellg();
     cin >> word;
}

There might be a better way to do this but this is all i could think of.

Ty, will see if I can work with it, but would like to also see if there is another way, this kinda confuses me (no offense)

Just out of curiosity, I was wondering if you would show me how you would perform the operation you want in c#.

I don't know how to, i just didn't know about the compare function in C++, i wasn't sure if some of these functions existed or not.

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.