fuddlepixelFuddlepixel coz tech is fun();

Steps involved with file handling in C

Let us discuss the various steps involved in C language for file management. Opening, closing, reading, writing and the concepts behind them!

By Shubham Ramdeo
21 December 2016 · 5 mins read
# c  newbie 

Steps involved with file handling in C

That’s basic steps involved in C for file handling!

Welcome to the Newbie Programmer Series. In the last part (here), we have discussed the basics of file handling. At the end we have also discussed the steps a programmer usually takes while handling files, or at least while using C language. In this part, we are going to discuss those steps in detail, each separately with their respective functions. So if you are new to this series, please go to the index (here) and read out all the previous parts so that you can easily understand what is going down below.

So we were discussing the steps involved with file handling. They can be sorted as:

  1. Creating File Variables.
  2. Opening the File
  3. Using the File
  4. Closing it

Let’s discuss them one by one.

Before doing files!!!

Before doing any kind of file management, remember that you have to include “stdio.h” header file. File handling is also a part of standard input output functions so they are accessible through this header file.

Creating the File Pointer

I have not discussed with you the term “pointer” in much detail, but you can find an introduction here. Pointer is basically : “A variable that points the location of memory”.

So, A file is something stored in computer’s memory. Instead of dealing with the file directly, we shall use a pointer. Whenever we will use the pointer it will point that file. Why we are doing so is a bit advanced and I think we should consider on files first rather going deep into memory!

To create a File Pointer, do this:

FILE *file_pointer_name;

So for example:

...
FILE *fp;
FILE *p1, *p2;
...

This will creat file pointers “fp”, “p1” and “p2”.

Opening a File

To open a file, you may do this:

file_pointer = fopen("file_name", "MODE");

What is file mode?

When we open a file, C language allow us to open in for the various ways of using it. We can write, or read, or append it. These are called as modes.

  • w : Writing Mode. A new file is created and opened for the writing purpose only. If that file already exist, still it will be deleted a new file will be created and opened for the writing purpose.
  • r : Reading Mode. If the file already exist, it will be opened just for reading only. If the file is not there, It will be regarded as an error.
  • a : Appending Mode. If the file exist, you can add data into it. If the file does not exist, a new file will be created.

With reading mode, you can only read and data is kept safe. With appending mode, the actual file will not be deleted so the data is kept safe and you can also add into it. With writing mode, the file will be deleted if it exist. Also with reading mode, if the file is not there it will be considered as error while writing and appending mode will create a new file.

But this will make the process hard if you want to read and write at the same time. So, we can add a ”+” To do reading and writing together. For example with “r+”, IF THE FILE EXIST, it will be opened for both reading and writing purpose. Note that the previous conditions of file’s existence still holds true.

So for example :

fp = fopen("test.txt", "w+");

This will always create a fresh new file, named “test.txt” and for both reading and writing purpose.

You can always open as many files as you want.

FILE *fp1, *fp2;
fp1 = fopen("file1.txt", "r");
fp2 = fopen("game.dat", "w");

Now ‘fp1’ is pointing to “file1.txt” for reading purpose, ‘fp2’ is pointing to “game.dat” for writing purpose.

Before doing anything else, let’s discuss how to close it first!

Closing a File

This is the easiest part!

fclose(file_pointer);

We have to close all the files that we have created.

fclose (fp1);
fclose (fp2);

Why close file?

We need to close a file because when we open it, the file takes up memory. So we close it to save space.

Using the files

So we have created the variable, opened the file and closed it. Now we will discuss how we will use it. Guess what should be involved with the basic functionality with the file ? What do we do with a notebook ? Writing and / or Reading. And so with the files.

We will discuss individual functions for handling input and output operations in detail in the next part.

But let’s do a quick example:

#include <stdio.h>
main()
{
 FILE *fp;
 fp = fopen("file.txt","w");
 fprintf(fp, "Hello, World!");
 fclose(fp);
 return 0;
}

We have included “stdio.h” header. Then we created a file pointer called “fp”. We then open a file, “file.txt” in “writing” mode. if it does not exist, it will be created. Then we used fprintf function to print “Hello, World!” in the file pointed by fp, which is, file.txt. Then we closed it. After running it, if you see the directory where this source file is compiled, you will find an another file “file.txt”. Open it in Notepad, it will show:

Hello, World!

Thanks for reading so far, we have discussed the various steps in detail. In the upcoming post we will discuss more input output operations like fprintf in detail. Remember that we have not discussed applications yet,  It will be done in the upcoming posts. So Stay Connected :)


Share this :



Update me weekly ✉

Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.

I'm in ♥


Or follow us
   


About author

Shubham Ramdeo

Shubham Ramdeo

Shubham Ramdeo is the founder of Fuddlepixel. He is a geek, a writer, involved with many technical and humanity projects.

Meet me!



Recent





Related

c 

newbie 

Top