The master programmer sat at his desk, gazing into the distance, his eyes seeing, and yet unseeing.
The lunch had been heavy.
His reverie was interrupted by the appearance of his close colleague, Ananda.
"Ah, Ananda, it is you," spake the master programmer, his very words oozing wisdom, "How fares your Tcl program to classify a list of file names based on their extensions?"
"The code is written, O master, and the output created," Ananda replied, "But strange are the lines of the output, for they are in reverse order of those in the input."
The master smiled. "Doubtless, Ananda, you have been storing your lines in a list, and then iterating over its elements in reverse order."
"No, master. My program simply takes a line from the input, processes it, outputs the result, and then moves on to the next, like the eternal soul that moves from one body to another ..."
The master shook his head. "If such be your code, Ananda, then it can only mean that you have scripted somewhere a seek command on one of your files."
"My program does not seek, O wise one. It is only I who seek an answer to this great mystery."
The master laughed. "Come now, Ananda. It is no mean task to reverse the order of lines even intentionally, and you would have me believe that you have done so by accident? Was it not just a few days ago that I explained to you: how the insertion of a line at the start of a document is merely an illusion perpetuated by text editors, while reality requires the entire file to be rewritten on disk?"
"Indeed, I do remember your teaching, master. But if theory and practice be in contradiction with each other, then which should one regard as reality, and which as illusion, O learned one?"
The master frowned. "I find your insistence both annoying and disturbing. Come, show me this magical program of yours, that I may rid you of this delusion once and for all."
"Gladly, master. ... In preparation for your visit, I have already cleansed my directory of all save my program and the input file. ... Here you see these 10 lines of input ... and here I run the program before your eyes ... and now see here in this output file that has appeared those 10 lines processed, but in reverse order."
The master blinked.
"Shall I open the program now, master?"
The master cleared his throat. "Unveil it, that I too may lift the veil from your eyes."
"As you wish - or tclsh - O master. Here it is."
The master breathed deeply. "Let's see... Your file is named output.txt
... search ... and this name appears only once here in this open
command, whose result is assigned to this variable ofp
... search ... and this ofp
is used only in this puts
command ... that prints this variable ... which is the result of this string processing ... on this variable ... which is set by this gets
command, reading from your input file handle ... inside this loop ... which ... ... ... does ... nothing else ... ... ... ?!"
Ananda sat silent. It was the first time that he had heard the master say, "?!". The punctuation marks rang in his ears.
The master scratched his head. He deleted the output file. He changed the contents of the input file. He ran the program. He saw the output. He cursed silently.
Ananda watched patiently.
The master closed his eyes.
"Are you meditating, master?"
"Be quiet and let me think!"
Where is the fault?
Continued:
After a few moments, the master opened his eyes. "One only has to inquire within, Ananda," he said. And then without a further word, his fingers moved swiftly across the keyboard.
"O great wielder of the keyboard," said Ananda, "I could only discern two presses of the Enter key amidst a blur. Pray tell me what two lines of code you did add."
The master smiled compassionately. "It is the self-analysis technique of the ancients, Ananda. Both the lines are simpleputs
commands to the console. One after thegets $ifp sInput
sayingputs "here 11111 reading ....... $sInput"
, and one before theputs $ofp $sOutput
sayingputs "here 11111 writing ....... $sOutput"
. The output of these two lines shall transcend the structure and logic of your program, and tell us what really it is attempting to do. Then either we shall know it to be reading backwards, or to be writing backwards, or perchance we shall find the numbering and sequence of reading and writing to be as it should not be, and thusly shall we realize where to look next."
Ananda watched in awe as the master ran the program, whose consciousness level had just been raised.
And lo! There appeared on the screen 20 lines in all: 10 for reading and 10 for writing, the reading and writing being in exact alternation with each other, and both sets in precisely the same order as the input! The only reversal to be seen was on the face of the master.
After a pause, the master smiled knowingly, as one smiles when one has truly known nothingness. "Such a perfect harmony between the reading and the writing," he murmured, "And yet, somewhere there is a disturbance in the yin and yang of this program. For though the writing on the console be in order, yet the contents of the output file are reversed."
"Ummm... master?" Ananda interjected, "I was thinking ... Maybe we should revisit that theory of inserting a line at the start of a file ..."
The master gave Ananda a stern glance. "It is no theory Ananda, it is verily the way of nature. One can only read, overwrite or append; one cannot insert. These are the three modes of nature: 'r', 'w' and 'a'. There is no 'i'."
Ananda sulked.
The master patted him. "No, Ananda, there are subtler forces at play here ..."
Notes:
1. This is based on an actual incident that happened with me at work last year. The dialogue, of course, is exaggerated, somewhat in the style of The Tao of Programming.
2. This originally occurred in a Tcl program on Linux, but I've confirmed that it also happens in equivalent C programs run on Linux or Solaris.
gets
, the string processing, and theputs
are all inside the loop? May we (correctly) make other assumptions about the structure of the code, such as that there isn't some other loop containing this one, that the file-opening happens outside the loop, etc.? $\endgroup$