How do you read a file backwards in C++?
I guess this is my first post.
Greetings to you all. I am eager to help building this wonderful community . I think the concept of this block-chain technology is rooted in our constant fight for freedom , love and meaning , values which I adhere to . Let us all then work and help each other, with hope that our struggle will bring a better future.
Looking back at what some would call an easy problem
Back in the day I received this problem in my computer science class:
A file with n numbers is given with the name "numbers.txt" . The range of numbers can vary , but they are always smaller than 1000 and n is a number bigger than 1000 and smaller than 1000000. Display the last odd number from the "numbers.txt" file.
When I first started to code something stuck to me : efficiency .
So... I started thinking about the most efficient way to solve the problem. Obviously I noticed that the probability of the last odd number being close to the end of the file is pretty big , so naturally I decided to read the file backwards.
I rapidly came to the conclusion that I never actually learnt such an algorithm ,so I tried to make on of my own. After a lot of failed attempts and 4 hours of consulting the wiki I finally came up with a clean code.( Never thought that manipulating the pointer in a file would be so hard)
So here it is in all it's might:
#include<iostream> #include<fstream> #include<conio.h> using namespace std; int main() { long int i,j,x,y; ifstream f("numbers.txt"); f.seekg(0,f.end); i=f.tellg(); for(j=1;i>0;i--,j++) { f.seekg(-j,f.end); f>>x; f.seekg(-j-1,f.end); f>>y; if(x==y&&x%2==1) {cout<<x; break;} } f.close(); getch(); }
An ongoing enigma
Even to this day a mystery still lingers in this code. The way in which I extract the numbers from the file is unnatural at best. Basically I move the cursor by one unit and when I move it again I get the same value by using the ">>" operator. And when I get the same value I know that I have a number there. Even to this day I can't understand why this is a thing but the program works just fine. Maybe someone more knowledgeable can tell me the science that takes place here.
An interesting result and an outlook for the future
This problem was my first challenge so to say and It still remains in my head as a turning point. After this every line of code looked different to me. This algorithm was beauty at it's purest in my eyes, a manifest of human expression and mystery. After this problem I always tried to bring all my algorithms to their canonical form - the form in which an algorithm can't be further simplified. I engraved this into my creed and I will always find beauty and joy in perfectly optimized programs. Looking forward I think I will try my best to find a career where optimizing and conjuring beauty out of thin air will play a major role.
✅ @tda36, I gave you an upvote on your first post! Please give me a follow and I will give you a follow in return!
Please also take a moment to read this post regarding bad behavior on Steemit.