×
 Introduction
Introduction to Newbie Programmer Series Introduction to Computer The Computer Programming Language Setting up the Computer for Programming
 Lets Get Started !
Beginning C Programming Variables a Handbag ! Basic Variable types in C Basic Formatted Output Basic Formatted Input
 Loops
Looping Repeating Concept Different Kinds of Loops Advanced concepts and trick with Loops
 Operators
Operators, An Introduction Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Miscellaneous Operators and Operators Precedence
 Controlling the Flow
Flow Charts Conditional Statements Using Goto and Label Break and Continue the loops Switch the Cases Some Practical Applications of Control Flow
 The program structure
The program Structure Functions More Concepts of Functions Scope of the Variables across functions How the C language itself Works ? Scope of the Variables across files Static Variables Auto and Register Variables #define PreProcessor Some more PreProcessors
 Arrays and Structures
Introduction to Arrays Arrays, some more Concepts Arrays, Cool Examples Structures Introduction Structure, Some more Concepts Structure, Some Examples
 File Management
Basics of File management in C Steps Involved with File Handling in C
 Miscellaneous
Inside Logic Gates – The Electronic Logic Binary Number System Mathematical Reasoning
Newbie Programmer
full book coming soon
by Shubham Ramdeo

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

Steps 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.

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 :) 

Please share this as much as you can so that we can get connected with more and more people who want to learn programming.

© Shubham Ramdeo

Site Map
with
© Shubham Ramdeo