Opening Files

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
srg_13

Opening Files

Post by srg_13 »

How can you open text (.txt) and .com files in an OS using C?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Opening Files

Post 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())
Every good solution is obvious once you've found it.
Schol-R-LEA

Re:Opening Files

Post 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.
kernel_journeyman

Re:Opening Files

Post 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.
Post Reply