Closed newboerg closed 7 years ago
@newboerg
What if there where SIMPLE read/ write functions for files?
Thank you for this proposal! Actually, I have been "pregnant" with this idea for some time, too. In a way, I have been waiting for someone to request it...
What are the main challenges:
At least add an Option to pipe the "Output to console" into a file.
Ah, well, I had hoped that the possibility to copy the text content from the console window would be sufficient for a while... ;-) I will think about such an option (would rather be a "tee" i.e. a spawning of the output where the executor messages would have to be filtered out from the file stream while the output window still shows all).
readFile() could return the file line wise per call, until of course EOF would be reached.
maybe do it like this, so ist working similar to Input
oh and fileRead() should be green then :)
I agree with you that it should be as easy as possible. I do not agree with you that open and close are neglectible. I think there is a lot students can and should learn from it:
So I suggest to introduce a set of procedures and functions using a simple integer value generated by the Structorizer (a unique counter) as file handle. The first three functions would open a file and produce a handle that can be used until fileClose() has been called with this number as handle.
fileOpen(path: String): Integer (for reading access)
fileCreate(path: String): Integer (for writing access, overwriting from start)
fileAppend(path: String): Integer (for writing access, writing from previous end)
fileWrite(handle:Integer; value: Object)
fileRead(handle: Integer): Object
fileClose(handle: Integer)
fileEOF(handle: Integer): Boolean
In files opened with fileCreate() or fileAppend() the routine fileWrite() can be used, in files opened by fileOpen() functions fileRead() and fileEOF() can be applied. If the opening fails then a handle value of 0 might be returned or possibly some negative number serving as error code. The use of an inactive file number would cause an error at runtime.
I just updated my previous comment - I had closed the issue by mistake instead of putting something to the begun text.
A demo for the reading of the values from a text file would look like this with my proposed approach: You may see by this design that I prefer a predicting EOF test (in contrast to the C function feof, which only detects the file end after a failed reading attempt has already discovered it). Internally this is less efficient, of course, bevause it requires a failing read attempt that has to be undone or buffered behind the scenes, but it is way more understandable and easier for the beginner learning to work with files. This approach is relying on built-in library functions and procedures rather than configurable "keywords", which could be highlighted. I don't see how we could level this difference without the simplifications you suggested but I don't regard as helpful.
Don't expect the prototype for this enhancement before Christmas, please.
Well, this Sir would be Great to have indeed.
Specification update: It turned ot, that it will be more practical to provide some more procedures and functions for a sensible working with text files:
fileOpen(path: String): Integer (for reading access)
fileCreate(path: String): Integer (for writing access, overwriting from start)
fileAppend(path: String): Integer (for writing access, writing from previous end)
fileWrite(handle: Integer; value: Object)
fileWriteLine(handle: Integer; value: Object)
fileRead(handle: Integer): Object
fileReadChar(handle: Integer): Character
fileReadInt(handle: Integer): Integer
fileReadDouble(handle: Integer): Double
fileReadLine(handle: Integer): String
fileClose(handle: Integer)
fileEOF(handle: Integer): Boolean
Some explanations:
fileWrite
will write an arbitrary value just as is without any additional separators, line feeds etc. This allows to compose arbitrary texts without Structorizer interference.
fileWriteLine
will do the same but add a newline character.
fileReadChar
will return the next character of the file (including a blank or newline character).
fileReadInt
will return an integer value if and only if the next token in the file is an integer literal, otherwise it will raise an error.
fileReadDouble
will return a floating-point number if and only if the next token in the file is a number literal (be it integral or not), otherwise it will raise an error.
fileRead
may return:
-17
) follows at the current reading position;3.6e17
) follows at the current reading position;{0, 25, foo, "text without commas", 6.9}
) follows at the current reading position;"This text, however, might contain commas, but no escaped quote"
) following at the current reading position;foo
) in any other case.fileReadLine
will always return a string comprising the (remainder of the) current line up to but not including the next newline character. The newline character will be consumed, though.
Updated API proposal is implemented and seems to work. More complex tests will have to be performed, particularly to verify robustness against different kinds of exceptions. No code export activity so far. Will have to be cared of before release 3.26.
disable the file i/o article on the page till features are active :-)
@newboerg As it is difficult to disable the uploaded User Guide passages without deleting them from the server, I did my best to mark them as preview. TO DO checklist:
OT: Is there a Roadmap for Structorizer?
Back to Topic: What if there where SIMPLE read/ write functions for files?
This would obviously produce Problems with the code Generators, but would open a lot of potential for teaching how to handle files (EOF,EOL and so on).
At least add an Option to pipe the "Output to console" into a file.