Hello and welcome back.
We're now going to discuss how to read and write files.
Firstly make sure you include SPDR Io Doc Hage and memory Doc Hage.
Okay so what we're gonna do then is I want you to do file F equals f open top forward slash test up
to 60.
And this is the file name we want to open and the SEC.
The second parameter is the mode.
So this could be like opening the file for writing open in the file for a pending or open file for reading.
So here we can go ah.
Which means we're open in this file for reading.
And if you now do if f and inside here do an explanation Mark just before the F and then do print f
failed to open file return minus 1.
Now what this will do is if it first open the file for reading it will output this message and leave.
So if we now compile that GCSE main that's C Dasha main run that you can see failed to open file and
that's because test or text does not exist in the directory that this executable is run because remember
don't forward slash means this directory means the current directory so in our case it will be the current
directory main that you see is being run in.
So if we now create a new file here and call it test or 60.
Now if we type Hello Weldon here and we now compile that again and run that you can see we see no error
message because it managed to open the file so down here just to f close F and what this does is it
closes the file.
So that is just to close the file handle basically when you finish with the file you close it.
So when I'm gonna read from the files so first I wanted to create a buffer call it chop off and put
20 in here for 20 bytes and we're gonna go mem set path 0 size of buff
so this will set our entire array to zero because when we read from the file we want there to be a null
terminated at the end.
Right.
So here now I want you to go f read I wanted to pass in B you f above and this is the element size so
f reads reads and blocks.
So for example if we put twenty for the elements size and put I don't know 30 for the element count
it'll read 30 elements in 20 by blocks so read 20 bytes and then that's one that's one element in Louise
another 20 bytes.
That's two elements and every returns the number of elements read.
So for example if you have an element size of 20 an atom and kind of one we we read 20 bytes but one
is returned because the block size is 20 all the elements size is 20.
So what we're going to do in here we're going to put the element sizes 1 because we want to read one
bytes at a time and we're going to put we're going to put 20 by here and then finally here goes our
false stream our open files.
So we put F and now if you now check to see if this is equal to zero and if it is we read nothing.
There was a problem
okay.
And now you can return minus one here.
So now we should be able to just print out our buffer here.
So if you do print f percent S which means that we are out putting a string.
So what'll happen is print f will go through our buff.
It'll output every single character until it finds out no Terminator and then it will stop.
So let's now compile that and we run that we see hello world.
So it's red.
Hello Wolf my file into the into this buffer here.
And then it's output is it.
Now let's take a look at the results of f read.
So if we go into J results equals f read and then do that.
And here we just two results equals equal zero.
Show the error.
And now in here if we output underneath here we go first.
We put a backslash end here for a new line.
If we then go print f percent I and then we do results.
And now we now compile that again and run that you'll see that outputs 12 which is the size of hello
world because it read 12 elements.
So if you're expecting a certain a certain size to be read then you could also check here that it is
equal to that.
And if it is not equal to that.
If it doesn't equal twelve if it does not equal 12 then we failed to read the full string that we wanted
for example.
But equals equal zero is sufficient for now.
So let's change the size here to 2.
So the element sizes too if we now compile and run that we can see Hello World.
But the result returned from F three to six because it read six elements because it was read in two
bytes at a time.
So I hope that makes sense.
If we put it in 12 here So reads the full thing and we want to read twenty blocks of twelve so it will
attempt to read it will attempt to read 240 bytes then what will happen is that we run that now
compile it first.
We now see one because it's red one block because it's red the full twelve bytes which was one block
with a 13 here and we can pile up.
Now you can see problem reading from file because 13 was the minimum amount of bytes had to read and
it couldn't finish reading that block.
So that was a problem.
So it returns zero because it couldn't read the the full block because remember the result that every
returns is the amount of blocks read.
And because our block size is 13 and we only have 12 bytes in the file it couldn't read the full block
so it read 0 blocks.
I hope that makes sense with F read.
So we're now going to discuss how you can write to files.
So let's get rid of all of this.
And I want you to change this mode here to w what w means.
It means we want to open this file for writing.
If the file exists overwrite it.
That's what that means.
So now if we now do f right and we did Hello worlds and in our element size was one because we we want
to read we want to write one bite at a time.
And then the element count is the size of Hello World.
I mean string length Hello World string Len Hello.
And then here we do F for fail if we now run that it will write Hello world to our file but let's just
do.
Hello World How are you instead.
And make sure you change this as well guys because it needs it needs to be able to write the full amount.
Right.
So now if we now compile that again and run that you can see we see no message.
But if you now go to test up to 60 you see Hello World How are you.
So we've essentially wrote to the file and again this works like F reads.
It will return the number of blocks read the number of elements read in our case the element sizes 1
and our element count is the size it is the string size of hello world how are you.
So that is how you can write files.
Let's now discuss how you can append files.
So if you change this flag this mode here to a that that means append.
So if we now compile and run that again you can see nothing show.
But if we go back to the file you now see Hello World How are you.
I will tell you if you run it a third time and we reopen that again you can see it again.
And if we keep running it it just keeps appending the file on and on and on.
So that is what the appendix for is for appending files w overwrite the file append depends to the end
of the file.
So another thing to discuss where append is.
It doesn't create the file if it does not exist.
So let's now discuss.
Plus R Plus opens the file for reading and writing but it does not create the file if it doesn't exist
w plus creates the file if it doesn't exist it will open it for reading and writing a plus opens the
file for appending and reading.