Getting started with C Language
Chapters
Working with Files in C
Files
A file is a place on disk where a group of related data is stored. Files are used for permanent storage of data. In the case of normal variable when the program is terminated, the data stored in the variable will be lost in C. we cannot keep large volume of data with ease, it is time consuming to enter the entire data. But, if we create a file , the information can be accessed using commands or functions to manipulate this files. C supports a number of functions that have the ability to perform basic file operations. They are
- Naming a file
- Opening a file
- Reading data from file
- Writing data to file
- Closing a file
There are two distinct ways to perform file operations. First one is low level I/O ,and second one is high – level I/O operations which uses functions in C’s standard I/O library. High level file I/O functions can be of two types:
- Text file
- Binary file
There are many functions which is used to handle this operations on files. They are
fopen () – is used to create a new file for use or to open an existing file for use
fclose () – is used to close a file which has been opened for use.
getc () – is used to read a character from a file.
putc () – is used to write a character to a file.
fprintf () – it writes a set of data values to a file.
fscanf () – it reads a set of data values from a file.
getw () – reads an integer from a file.
putw () – writes an integer to a file.
fseek () – sets the position to a desired point in the file.
ftell () – gives the current position in the file. (In terms of bytes from the start)
rewind () – sets the position to the beginning of the file.
Defining and opening a file
If we want to use a file for storing data in secondary memory we must specify certain things about files to the operating system. They are
- Filename
- Data structure
- Purpose
Where filename is a string of characters that make up a valid filename for the operating system. It may contain two parts , a primary name and an optional period with extension.
Example :
Program . c
Data structure of a ‘file’ is defined as file in the library of standard I/O function definitions. Therefore, all files should be declared as type ‘file’ before they are used. File is a defined data type.
Opening file
When working with file, we need to declare a pointer of type file. This declaration is needed for communication between file and program.
FILE *ptr;
Opening a file is performed by the library function fopen().
Syntax :
ptr=fopen("fileopen","mode")
Example :
fopen("C:\\cprogram\addition.txt","w");
C:\\cprogram\
is the location to create the file addition.txt
"w" represents the mode for writing.
Here, the addition.txt file is opened for writing mode.
Opening Modes in Standard I/O
r - Open for reading.If the file does not exist, fopen() returns NULL.
w - Open for writing. If the file exists, its contents are overwritten. If the file does not exist, the file will be created.
a - Open for append. That is data is added to end of file.If the file does not exists, it will be created.
r+ - Open for both reading and writing. If the file does not exist, fopen() returns NULL.
w+ - Open for both reading and writing. If the file exists, its contents are overwritten. If the file does not exist, it will be created.
a+ - Open for both reading and appending. If the file does not exist, it will be created.
Closing a File
Whenever a file is opened for any purpose it must be closed after reading or writing of a file. Closing a file is performed using library function fclose().
Syntax :
fclose ( file- pointer);
Example :
fclose (ptr);
Here ptr is the file pointer associated with file to be closed.
Input output operation on files
When a file is opened there are a set of standard input or output functions associated with these files.
getc () and putc ()
It is the simplest file I/O function in C. this handle one character at a time. If we open a file with write mode and file pointer ptr then
putc (c , ptr);
it writes the character contained in the character variable c to the file associated with file pointer ptr.
c = getc(ptr);
it is used to read a character from a file that has been opened in read mode.
getw () and putw ()
Both are integer oriented functions. similar to getc () , putc () functions and are used to read or write integer values.
Syntax :
putw ( integer , file-pointer );
getw ( file-pointer);
fprintf () and fscanf ()
It is used to handle a group of mixed data simultaneously.
Syntax :
fprintf ( file-pointer, “control string” , list);
fscanf ( file-pointer, “control string”, list);
Here control string contains output specifications for the items in the list. The list may include variables , constants and strings.
Example :
fprintf ( ptr, “%s %d %f”, name ,age,7.5 );
fscanf ( ptr, “%s %f” ,item , &price );
Program to read data from the file
#include <stdio.h>
#include <process.h> // to use exit function void main() { int n; FILE *fptr; if ((fptr=fopen("C:\\program.txt","r"))==NULL) { printf("Error! opening file"); exit(1); } fscanf(fptr,"%d",&n); printf("Value of n=%d",n); fclose(fptr); }
Binary Files
A file is classified into text file and binary file , depending upon the way file is opened for processing. The text mode will be insufficient , when a large amount of numerical data is to be stored. In such case binary file is used .The binary files is similar to text files with few differences in opening modes reading and writing . Opening modes of binary files are rb, rb+, wb, wb+,ab and ab+. The only difference between opening modes of text and binary files is that, b is appended to indicate the file is binary file.
Program to write data to the file
#include <stdio.h>
#include <process.h> // to use exit function
void main()
{
int n;
FILE *fptr;
fptr=fopen("C:\\program.txt","w");
if(fptr==NULL){
printf("Error!");
exit(0);
}
printf("Enter n: ");
scanf("%d",&n);
fprintf(fptr,"%d",n);
fclose(fptr);
}
Description
This tutorial introduces you to C language. Many tutorials has been written. We are writing this tutorial to be brief and to the point, so that you concentrate on what matter, rather than finding yourself in a mess. We want you to get a start and build form knowledge you will gain from this tutorial
Environment
We will cover environment needs as we progress through this tutorial. At the very least you will need a PC with windows or Linus operating system.
Audience
Programmers new to C Language
Learning Objectives
Learn basics of C language and build from that knowledge.
Author: Subject Coach
Added on: 3rd Jan 2015
You must be logged in as Student to ask a Question.
None just yet!