File Management
File Management
1
Introduction to File Management
• Some application require input to be taken from a file and output is
required to be stored in a file.
• The ‘C’ language provides the facility of file input-output operations.
• The standard library in C has many file I/O functions.
• File is a place on disk where a group of related data is stored.
• Sequence of steps for operating on a file:
• Naming or creating a file
• Opening a file
• Reading or writing to the file
• Closing the file
2
Opening a file
• While working with file, you need to declare a pointer of type file. This
declaration is needed for communication between file and program.
• FILE *fp; -- declaration
• Opening a file is performed using library function fopen(). The syntax for
opening a file in standard I/O is:
• fp = fopen (“filename”, “mode”);
• For example, fp = fopen("E:\\cprogram\\program.txt","w");
• In the above statement, fp is a pointer to the data type FILE. The above
statement opens a file named filename and assigns an identifier to the FILE type
pointer fp. This pointer which contains all the information about the file, is
subsequently used as a communication link between the system and the
program.
• The function fopen returns a pointer to the opened file stream. The parameter
filename is the name of the file to be opened. The mode string can have one of
the values showed in the table.
3
Opening a file
• Mode string for fopen( )
Mode Description
w Open file for write purpose. Create for writing and if the file already exist,
it will be overwritten
a Append, open for writing at end of file (create for writing if doesn’t exist)
a+ Open for append, open for update at the end of the file.
4
Closing a file
• The file should be closed after reading/writing of a file. Closing a file is
performed using library function fclose().
• fclose(file_pointer);
• For example, fclose(fp);
5
Input/output operations on a file
Function Name Operation
fopen( ) Open the file for use
fgetc( ) Read a character from the file (text file)
fputc( ) Writes a character to the file (text file)
fclose( ) Close a file, which is open by file pointer
fprintf( ) Write a set of data values to files (text file)
fscanf( ) Read a set of data values from files. (text file)
fputs( ) Write string to file (text file)
fgets( ) Read string from file (text file)
fread( ) Read records (sequence of bytes) to the file. (binary file)
fwrite( ) Write records (sequence of bytes) to the file. (binary file)
getw( ) Read an integer from a file
putw( ) Write an integer to a file
6
fgetc( ) and fputc( ) functions
7
fscanf( ) and fprintf( ) functions
8
fscanf( ) and fprintf( ) functions
9
fgets( ) and fputs( ) functions
• The C library function char *fgets(char *str, int n, FILE *stream) reads a line
from the specified stream and stores it into the string pointed to by str. It
stops when
• either (n-1) characters are read
• the newline character is read
• or the end-of-file is reached, whichever comes first.
• Following is the declaration for fgets() function.
• char *fgets(char *str, int n, FILE *stream)
• Parameters
• str -- This is the pointer to an array of chars where the string read is
stored.
• n -- This is the maximum number of characters to be read (including the
final null-character). Usually, the length of the array passed as str is used.
• stream -- This is the pointer to a FILE object that identifies the stream
where characters are read from. 10
•
fgets( ) and fputs( ) functions
• The C library function int fputs(const char *str, FILE *stream) writes a string
to the specified stream up to but not including the null character.
• Following is the declaration for fputs() function.
• int fputs(const char *str, FILE *stream)
• Parameters
• str -- This is an array containing the null-terminated sequence of
characters to be written.
• stream -- This is the pointer to a FILE object that identifies the stream
where the string is to be written.
• Refer program fputs.c
11
fwrite( ) function
12
fread( ) function
13
getw( ) and putw( )function
14
Random File Operations
15
ftell( ) function
16
rewind( ) function
17
fseek( ) function
18
Command line arguments
• Command line arguments are parameter or arguments passed to a program
when it runs from command prompt.
• Example: C:\tc\bin > file1 a.txt b.txt
• Where file1 is the program name where executable code of the program is
stored, and a.txt is the first argument passed to the program and b.txt is the
second argument passed to the program.
• These arguments are recognized into program by main( ) method. For
handling command line arguments, main( ) function has to be written with
two arguments are shown below:
• void main (int argc, char *argv[ ])
• argc is argument count and argv array stores arguments passed to
program.
• For example, argv [0] = file1, argv[1] = a.txt, argv[2] = b.txt
• The first parameter in the command line is always the program name
and therefore argv[0] always represents the program name.
• Refer the program commandline.c
19
Thank you!!!
20