 Hi everyone, in this video we will learn about file.io in C programming language. So I have written this file.io.c program, let's open it with visual code. So this is the overall code, let's go over the code. So here's the main program. The very first system code that we use is the open system code, which is used to open a new file. So what all argument does it take? The first argument that it takes is the part to the file. Then it takes certain flags, for instance here we have used this rdwr flag which means we want to both read and write using this file descriptor. And second flag that we have used is ocreate which means if the file doesn't already exist then we want to create the file. And the last flag is the truncate flag which means if the file already exists then we want to clear all its contents. And finally the last argument is an optional argument and it is used only when we create a file. It is used to set the permissions of the file. So here we have given read, write and execute permission to the user. And this open system code returns a file descriptor which we store in this fd variable. So what happens when we make this open system code? The process will create the new file, it will allocate a new inode and add a mapping from the file name to this new inode number in the parent directory. And then it will copy this inode into memory from the disk. So now that we have inode in the memory it will create a new entry in the open file table which will point to this in memory inode. And it will also add a new entry in the file descriptor array for this process. And that file descriptor array entry will point to this open file table entry. And then it will return the index in that file descriptor array as the file descriptor. So if it returns a negative value that means that there was some error and we print out unable to open the file. Otherwise we print out file opened with this as file descriptor. Now we use the read system code to read from an open file. Firstly we allocate some region where we can read the contents and then we use this read system code to read the contents from the file descriptor. So read system code takes three arguments. First is the file descriptor itself. Second is the pointer to the region where it can store the data. So we have used this C variable here. And finally the number of bytes which we want to read. And this read system code returns the number of bytes that were actually read. So for instance if a file has just 5 bytes then it will return 5 instead of 10. So what happens when we make a read system code? On making this read system code the process will use the file descriptor array index and access the open file table entry. And based on that open file table entry it will access the inode. From that inode it will see what all are the data blocks that store the corresponding data that we want. And it will check if that data block is already there in disk buffer cache. If it is not there in disk buffer cache then it will issue commands to the hard disk to pull the relevant data blocks into the disk buffer cache and then it will copy the relevant contents from the data block to the region pointed by this address. And finally it will return the number of bytes which were read. So if it returns an active value that means there was some error. And if it returns 0 that means that we have reached the end of file. So we print out the end of file reached. Otherwise we print out that these many bytes were read and we print out the contents which were actually read. Now we use the write system call to write this hello word in this file. So write system call also takes first argument as the file descriptor. Second is the pointer to memory location from where we want to copy the data and write to the file. And the last argument says what is the size of data that we want to write. So this write system call also returns the number of bytes which were actually written to the file. And what happens when we make this write system call. Based on the file descriptor the system will access the relevant data block where we want to write the data. And in case we want to write beyond the end of file it will allocate a new data block and add its number to the file inode. And then it will pull this data block into the disk buffer cache and then copy the data from this location to the data block. And finally return the number of bytes which are written. So if it returns a negative value that means that there was some error otherwise we print out that it has written these many bytes to the file descriptor. Now after writing to the file descriptor the offset inside the inode will point to the end of file. And if you want to read the data again we need to use the cx system call to again reset the offset to zero. Now we use the lc function which takes in the file descriptor the offset that we want to set and if we want to set this offset in absolute way or we want to set it relative to the current offset position. After setting the offset back to zero we use the read system call to again read contents from this file descriptor. We try to read 15 bytes but because we have just written 12 bytes it will just read these many bytes and it will print out the contents. Finally we use this close system call to close the file descriptor this will remove all the entries which are made in the open file table and the file descriptor array and it will return zero in case of success otherwise if it returns an active value that means there was some error and that would end the program. So let's compile and run this program so I will open a terminal and compile this program gcc file io.c and it creates the i.out executable let's run that executable so we can see that it opened a new file with file descriptor three and why is it three because zero one two are already used for standard in standard out and standard error and as expected it prints out end of file because there is no data initially in this new file then it writes 12 bytes to the file descriptor and finally it again reads that 12 bytes which were written there and the contents are hello world and then it closes this file descriptor. So let's see if we have that test.txt file here so we have this test.txt file and if we check its contents we can use cad text.txt so the contents are hello world so that's it for this video thanks and have a nice day.