0% found this document useful (0 votes)
43 views8 pages

Files in C 4

This document discusses how programs interact with files on a computer system. It explains that files are stored on a hard drive in blocks of data and accessed through system calls. A program uses library functions that wrap these system calls to open, read, write and close files. The operating system controls access to files and hardware devices, only allowing interaction through defined system calls.

Uploaded by

abhijit1303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views8 pages

Files in C 4

This document discusses how programs interact with files on a computer system. It explains that files are stored on a hard drive in blocks of data and accessed through system calls. A program uses library functions that wrap these system calls to open, read, write and close files. The operating system controls access to files and hardware devices, only allowing interaction through defined system calls.

Uploaded by

abhijit1303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Programs using files

Learn various C library functions


to trasfer data
between

Files and
program’s memory(i.e. variables, malloced memory,
etc.)
How files get managed?
● What we see is a logical abstraction as a tree
● Is that how files are stored?
● Files are stored on hard drive
● Every file is essentially some bit sequence (or byte sequence) stored on
the hard drive
● The hard drive is a mechanical device, using magnetic physics to store
data
● A software called Disk device driver knows how to store data on
the hard drive
● In general, user programs (like yours) are not permitted to access
any hardware (mouse, keyboard, memory, disk, etc.)
● Do you know any C language feature which allows you to access them?
System Calls
● The only way a user program can interact with a
hardware device is by requesting the OS to do the
desired task
● The OS provides functions for this
● These functions are called “System Calls”
● Your C library has wrapper functions for these system
calls
– E.g. printf() ! --> It accesses the screen!

Layered Design
int main() {
int x, y, z; User programs/
scanf(“%d%d”, &x, &y); application/
z = x + y; libraries
User Program
printf(“%d\n”, z);
return 0;
}

int scanf(char *str, void *a, void *b, ...) {


/* do something with str */
x = read(0, a, ...) ;
} C Librarry
int read(int fd, ... ) {
sys_read(fd, a,...);
...
} system call interface
int sys_read(int fd, char *buf ) {
ask the disk/keyboard driver to read the data
send data back into buf; Operating system
}
Device_driver() { talks to hardware and back }
Operating System and Files
● All I/O devices are under the control of Operating
System (OS)
● OS provides “system calls” to do I/O
● C library has wrapper functions over these system
calls
● For example: getchar(), printf(), scanf(), open(), fopen(),
read(), close(), fclose(), fopen() etc.
Some key concepts and misconceptions
about files
Remember:
You always need a program to open a file. The
program requests the OS to do I/O operations (open,
read, write, etc) on the file.
A file never opens itself. “YOU” don't open a file.
Some program opens a file.
We create files using programs. We access, modify,
delete files using programs.
Opening and accessing a file with a
program
#include <stdio.h>
#include <fcntl.h> C Library
int main() { Functions for
FILE *fp; Accessing
char data[1024]; Files. They
request the OS
fp = fopen("/etc/passwd", "r"); to carry out the
fscanf(fp, "%s", data); specific tasks
printf("%s\n", data);
fclose(fp);
return 0; Accessing files using a program: Steps
} 1. Request the OS to “open” the file:
requesting access, getting a token for
access
2. Request the OS to “read/write/seek”, etc.
In the file. Transfer data from hard disk to
your variables in the code and vice-versa
Your use of files so far ..
● You click on a file and the file opens itself!
● Wrong !
● What does exactly happen when you “click” on a file?
1. Your click on a file is read by a program (e.g. your ‘File
Browser’).
2. File Browser figures out the appropriate program for opening
your file (e.g. gedit for a .c file or vlc for a .mp3 file)
3. File Browser requests the OS to run the appropriate program
with the file name in argv[ ]
4. The appropriate program opens the file (using fopen( )/open( ))
and shows to you (using fread( )/ read( ), etc. )

You might also like