Welcome! Looking for a challenge? Here you go...
Imagine...
We'll assume that you'll have a way for the program's user to enter a number. If he/ she enters 1000, the program will jump into the text file 1000 bytes from the end of the file... or from the start of the file, if you want to write the program that way around.
If a user said "Jump to byte 1000", it would just be an unlikely coincidence if 1000 bytes from the end of the file happened to be at the start of a line. Part of a line is of no interest, so after the jump into the file, the program will skip past the rest of the line that the starting point specified.
"Simple text file": We'll assume we're working with a file that contains only bytes used for printable ASCII characters (including the $20 for a space), and the CR ($0D) and LF ($0A) "characters".
A CR/LF pair... in that order... will define the end of a "line".
There may or may not be a CR/LF pair after the last line of printable characters at the end of the file.
The free-to-try (indefinitely) text editor from Textpad is an excellent tool for preparing such files. (There are many ways, of course.) It also allows you to look at the bytes in the file. (Specify "Binary" mode while opening the file.)
A suitable simple text file is provided here. If a download doesn't start when you click on the link, please contact me. (Details at bottom of page.) It will download as a zip, not because it is long, but because otherwise browsers tend to display the file. Saving it from a browser would probably change it from a simple text file into something more complicated than we want.
Speaking of "simple text file", why have I restricted this to them? The use I can see for the tools which could arise from success with this challenge would be helpful to people who have written software to log data. The data log files are likely to be simple text files. (And if they aren't, what you learn in this challenge will help you get started with reading more complex files.)
For the sake of the challenge, we'll say that the program should read and display three lines from the file. (The program should be written to make it easy to use it as a basis for code to go "inside" a bigger program where how many lines are read is determined differently, and the bigger program has a chance to process each line as it is read.)
Hmm. That shouldn't be hard, should it?
For doing it in Lazarus, see...
https://www.freepascal.org/docs-html/rtl/system/seek.html
The "fun" starts when you make sure your code handles all boundary cases well.
And there are a few other "Gotchas" to entertain you. Well... there were things that Got me.
-----
A human gets to specify how far back in the simple text file the program should go for its starting point.
(Optional bonus points: By adding only ONE LINE of code, plus a few radio buttons, give the human the choice of specifying whether the number supplied should give the starting distance (in bytes) from the START, or from the END of the code.)
For the sake of the demo, have the program list three lines from the file in a TMemo object.
I hope the challenge is already pretty clear to you. You can download a zip file containing a program that does these things, if you wish. (You'll have to extract the .exe from the .zip before you run it.) The zip holds just the .exe you need, and that's all you need, apart from the file from which it will read. WHICH file it will read from is hard-coded. You can of course change the contents of the file. I would suggest, though, that you start with the sample data I mentioned earlier. It is in the format that I specified. Put the datafile in the same folder as the .exe.
I would be very happy to mention successful completions of the challenge here. Contact details below. By all means, please feel free to use the contact details also to make suggestions, ask questions.
I hope schools will use this challenge with students studying programming.
Search across all my sites with the Google search...
Use this to search THIS site.... (Go to my other sites, below, and use their search buttons if you want to search them.)
(Alas... my Lazarus material is split across two sites... SheepdogGuides.com and WYWTK.com. Sorry!)
This search engine is "old skool"- it merely looks for the words you type, so....
* Spell them properly.
* Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....
Please also note that I have three other sites, and that this search will not include them. They have their own search buttons.
My WhatYouWantToKnow site- https:/wywtk.com.
My SheepdogSoftware.co.uk site.
http://www.arunet.co.uk/tkboyd/index2.htm: My site at Arunet. (Sorry.. not httpS accessible)
To email this page's editor, Tom Boyd.... Editor's email address. Suggestions welcomed! Please cite "lt8n-chall-read-lines.htm".
Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes.
Click either icon to run its test. Be prepared to enter URL of page to be checked by hand when you reach the validation tool.
If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.
....... P a g e . . . E n d s .....