• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Reading in a file on a FAT partition

Krakerjak

Senior member
When reading a file on a FAT partition do you have to read in the exact amount of data specified in the root directory entry (file size)?

Or, can you just read in data cluster-by-cluster until you read in the EOF cluster even if it only fills up part of that cluster and you read in some blank data as part of the file?
 
Well, the total size (or is that the remaining size, modulo the size of the clusters on the partition? I'd have to look it up, sorry) is stored in the directory entry. The FAT cluster-chain is a linked-list, terminated by 0xFFs. There can be remaining data left over in the end of the cluster, that's why when CHKDSK or SCANDISK recovers "lost clusters" and turns them into those FILE0001.CHK files, there is garbage at the end.

Also, some DOS apps, when dealing with sequential (text) files, will append a DOS EOF character (CTRL-Z, 0x26) at the end. Not all editors do that, and some will strip that when reading the file, and then write it out without it.

Interesting little bit of trivia - there is a bug in the sequential file read/write APIs in MS-DOS 5.0. If you attempt to read or write chunks of *exactly* 64KiB in size, sometimes a carry flag gets lost somewhere in DOS's internals, and the DOS file-position ptr won't get properly incremented. In essence, if you write two 64KB chunks, the second may overwrite the first. The suggested workaround is to read and write data in 32KB chunks instead. I saw that documented a long time ago, and I didn't believe it, until it finally happened to me. That's what finally got me to upgrade to MS-DOS 6.x.

 
Back
Top