tags:

views:

32

answers:

2

Hello,

I want are the steps that an application takes inorder to open the file and allow user to read. File is nothing more than sequence of bits on the disk. What steps does it take to show show the contents of the file?

I want to programatically do this in C. I don't want to begin with complex formats like word/pdf but something simpler. So, which format is best?

A: 

Start with plain text files

mfeingold
A: 

If you want to investigate this, start with plain ASCII text. It's just one byte per character, very straightforward, and you can open it in Notepad or any one of its much more capable replacements.

As for what actually happens when a program reads a file... basically it involves making a system call to open the file, which gives you a file handle (just a number that the operating system maps to a record in the filesystem). You then make a system call to read some data from the file, and the OS fetches it from the disk and copies it into some region of RAM that you specify (that would be a character/byte array in your program). Repeat reading as necessary. And when you're done, you issue yet another system call to close the file, which simply tells the OS that you're done with it. So the sequence, in C-like pseudocode, is

int f = fopen(...);
while (...) {
    byte foo[BLOCK_SIZE];
    fread(f, foo, BLOCK_SIZE);
    do something with foo
}
fclose(f);

If you're interested in what the OS actually does behind the scenes to get data from the disk to RAM, well... that's a whole other can of worms ;-)

David Zaslavsky
claws