Open, Read & Close A File (fopen(), fread(), fclose())

3 minutes
Share the link to this page
Copied
  Completed
A better method to open files is with the fopen() function. This function gives you more options than the readfile() function. The fread() function reads from an open file. The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read. The fclose() function is used to close an open file.

Transcript

Hello, in this video we are going to look up opening reading and closing a file using the F open function which opened the form using the F read function which allows us to read the content from it. And once we've done you know handling the fall, it is good practice to close it was basically removes it out of the memory so you know frees up some memory. Okay, so to do this, this is a number alternative Darrell covered in the previous video, which has its limitation so we're not going to use this method anymore. Um, but the father says file dot txt. Okay, so first of all, what you want to do is actually read the file for dollar file equals F open. I mean, you want to open the file, and then you specify the path.

So we'll say file dot txt, put a semicolon and obviously if the file path is different, like if it's in a folder, feel free to put like a folder name for slash in there, wherever the where the file is. Mine directly in line with the index. php file server. So we're going to actually read this file now. So we're going to say dollar process equals s read. And in, not, Fred f read, you need to put the file, then we need to put how long the file is and an easy way of, you know, figuring out how long it is you just use the function file size.

And here again, you put file dot txt or the path of it. And finally, you need to close it by doing f close. That's for the fall that you're plugged in. So as we run this we're looking Okay, so get era or warning online free and saying Expect at least two parameters. Yep, that is right. And the next parameter is how we want to basically, you know, open the file.

So R is read only w will be right only a is for opening a file for right only as well, but the existing data in the file is preserved. So it's basically a pointer, actually for creating a new file. And you know, there's a bunch of other varieties of this as well, but I like to provide a link where you can see all of the different all the different different ways of opening the file, so that way you can decide which way is best for you for this video, we only need our which is so that's your fix. We only want to fix all the one is fine. I think that's a good reason. We should always go for solving your warnings any errors, you know, one after another in a linear fashion because such kind of errors might just be triggered by the first error.

Okay, so Now it's doing nothing because we haven't actually printed anything out. So let's print it out. So let's print out all the process or echo it out. And there we go. So there we go. We have it printed out.

We can use it wherever we want them, so it doesn't automatically just get pumped out. We don't have the annoying little character at the end, which is part of the one of the ASCII characters. Still not the best, because we have it formatted formatted on separate lines here, but here, it isn't formatted on separate lines. But don't worry, we are going to cover that in more depth in a separate video. But this is, you know, opening reading and closing a file so it's a good base, good foundation for going forward. Any questions?

Feel free to drop me a message. And as usual, I look forward to seeing you in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.