Page 1 of 1

Opening Files

Posted: Thu Sep 09, 2004 5:47 am
by srg_13
How can you open text (.txt) and .com files in an OS using C?

Re:Opening Files

Posted: Thu Sep 09, 2004 6:12 am
by Solar
You mean in kernel space C? By using whatever file handling routines you have implemented in your file system.

In user space, you traditionally use what <stdio.h> gives you (fopen(), fscanf(), fread())

Re:Opening Files

Posted: Thu Sep 09, 2004 9:16 pm
by Schol-R-LEA
Let us be precise in this: do you mean "how do I read from and write to the disk?", "how do I write a filesystem?", or "how do I open a file in standard C?"

The first will require you to study the floppy and hard disk interfaces, and write the appropriate drivers. RTQL and the FAQ for the appropriate references, and perhaps read "How to Ask Questions" as well.

For the second, that will depend on the filesystem you use, and how you intend to implement system calls and drivers in general. Do What Thou Wilt.

The answer to the last one is to use the standard I/O functions; if you need more details, inquire in the general forum, or consult your pineal gland.

Re:Opening Files

Posted: Thu Sep 09, 2004 11:42 pm
by kernel_journeyman
It might be worth a mention that there's no special way of opening a text file or a com file in an OS. Ideally the OS is full of mechanism and lacking in policy. It's up to usermode standard libraries to impose a policy, for instance seeing a file with structure.

The OS sees nothing but a bunch of bytes in some areas on a disk (actually, it doesn't see them, strictly speaking, because the filesystem code does that. And it too does not see anything but an array of bytes of some length.)

Reading from a file (any file) is reading the bytes on the file's disk blocks into a buffer in memory. For this, you need a disk driver, a filesystem, and some library functions to open/close files, read/write files and seek to offsets within files.