Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why can't you assign file variables like normal ones?

Status
Not open for further replies.

lionelhill

Technical User
Dec 14, 2002
1,520
GB
Just a question that's more of historical interest than anything else, but I'd be very interested if anyone knows:

Why is it that in Turbo Pascal 6.0 you cannot give the value of one file variable to another:

var
a : file;
b : file;

begin
assign(a, 'myfile.ext');
...
a := b; {Not Allowed! }
...

But you can embed a file in a record, and assign the whole record, and then it works as expected (therefore it is physically possible):

type
MyRec = record
thing : file;
end;

var
a : MyRec;
b : MyRec;

begin
assign(a.thing, 'myfile.ext');
b := a;
reset(b.thing, 1; {works fine!}
....

I'm intrigued why the writers of TP felt a file should not be treated like any other variable.
 
File handles are file addresses that are made with the operating system. Realistically it makes no sense to assign values of file handles back and forth. I would say what you are observing with record types is something the compiler is not catching onto but is problematic in any sense.

If you want a quick answer, they weren't DESIGNED to be treated like any other variable. Variables are memory resources, file handles are DOS operating system resources. It just doesn't work that way.
 
Thanks for the reply!

I agree with you that it's not essential to be able to reassign file variables. The place where I would have used it was a unit that added a series of assign-like procedures and functions for treating zipped-together files as though they weren't zipped. You hand the unit a name of what zipped sub-file you want to open, it returns a number corresponding to the sub-file (a sort-of artificial file-handle), which can then be used by subsequent calls to other functions and procedures to read the sub-files in various ways.
Within the unit it would have been quite handy to reassign files in the same way that you might have a pointer pointing to some structure, but still set another pointer to point to the same structure for convenience of manipulating it locally.

I'm biassed by assembler on this; in assembler, if I call a dos interrupt to do open a file, I receive the file handle back in the form of a number. It's merely a word that I can store where I want and treat how I want, and when I want to use the same file again, I return that number to the OS. It could be regarded as an array subscript into an array of physical locations on the disk (the physical locations and the array are all the concern of the OS and none of my business), allowing the operating system to translate my request for a file into a physical place from which the data can be retrieved.
In pascal, I often assign array subscripts to other variables - why not files too?
(side note: Actually I think TP 6.0 still uses the original DOS file information block rather than the handle, because the handle's just a word as returned by interrupts, but a file variable is 128 bytes, I think. Another good reason not to have too many of them!)

 
We are moving into a different topic somewhat. Yes you can do an array of file types under Pascal, but I'm not sure what you mean otherwise. You can do a seek() with an untyped file and arrive at a particular location. As far as the array of file types goes, I've done that with regards to creating a huge amount of temp files for processing purposes.

So you can have structures of files and make them work, it's just assigning values back and forth to the file variables usually gets dicey IMO.
 
Yup, thanks.

Sorry I haven't been here much so it took me a few days to see your reply. Actually your previous reply, too, set me thinking. Because TP originated in the days of file-control-block files, early DOS, the file information is held in the variable; that means if you assigned file variable "a" to file variable "b" and then read from one of them, the other would have incorrect information. With the file handle system the data are held by the OS, so both file variables would be up-to-date. Obviously it wouldn't be good to have a situation where the behaviour of a high level language depended on implementation of the OS; as you say, the situation would get very dicey, which probably explains TPs attitude to assigning file variables.

I'm sure the assignment within records is merely an accident of efficient copying of records; you just copy the whole record as a lump of memory, irrespective of its structure, knowing only its size.

Thanks for putting me in the right direction.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top