100% found this document useful (1 vote)
101 views20 pages

File Management

1. File management in C involves opening, reading, writing, and closing files using standard library functions. Files store related data on disk. 2. The fopen() function opens a file and returns a file pointer, which is then used with functions like fread(), fwrite(), fgets(), fputs() to read from or write to the file. 3. Other file I/O functions include fclose() to close the file, fscanf() and fprintf() to read and write multiple data types, and ftell(), fseek(), rewind() for random access within a file.

Uploaded by

Rushil Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
101 views20 pages

File Management

1. File management in C involves opening, reading, writing, and closing files using standard library functions. Files store related data on disk. 2. The fopen() function opens a file and returns a file pointer, which is then used with functions like fread(), fwrite(), fgets(), fputs() to read from or write to the file. 3. Other file I/O functions include fclose() to close the file, fscanf() and fprintf() to read and write multiple data types, and ftell(), fseek(), rewind() for random access within a file.

Uploaded by

Rushil Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

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

r Open file for reading only

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)

r+ Open an existing file for update (reading or writing)

w+ Create a new file for update. If it already exists, it will be overwritten

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

• The function fgetc( ) and fputc( ) are used to perform character


reading and writing from files. Assume that a file is opened with
mode write and file pointer fp1.
• Following statement,
• fputc(c, fp1);
• Where fp1 is file pointer and c is character type variable, which write
character c at fp1 position in the file.
• Similarly, the fgetc( ) reads one character at a time from the file
opened in read mode and moves the file pointer to next position.
• c = fgetc(fp);
• Where c is a character type variable, fp is a file pointer.
• Refer the program fgetc.c , fputc.c & copy.c

7
fscanf( ) and fprintf( ) functions

• For handling group of different data types from file at a time,


fscanf( ) and fprintf( ) function is used. The general format of the
fscanf( ) is given below:
• fscanf(fp, “control string”, &arguments);
• Where fp is a file pointer associated with file that has been opened
for reading. The control string contains output specifications for the
items in the list. The list may include variables, constants and string.
• For example, in the following code,
• int a;
• char b;
• fscanf(fp, “%d %c”, &a, &b);
• Here, a and b are variables in which data is read from file into variables.

8
fscanf( ) and fprintf( ) functions

• The general format of the fprintf( ) is given below:


• fprintf(fp, “control string”, list);
• Where fp is a file pointer associated with file that has been opened
for writing. The control string contains output specifications for the
items in the list. The list may include variables, constants and string.
• For example, in the following code,
• fprintf(fp, “%s %d”, city, total);
• Here, city is an array variable of type char and total is an int
variable.
• Refer the program fprintf.c and fscanf.c

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

• The fwrite() function is used to write records (sequence of bytes) to


the file. A record may be an array or a structure.
• Syntax of fwrite() function
• fwrite( ptr, int size, int n, FILE *fp );
• The fwrite() function takes four arguments.
• ptr : ptr is the reference of an array or a structure stored in memory.
• size : size is the total number of bytes to be written.
• n : n is number of times a record will be written.
• FILE* : FILE* is a file where the records will be written in binary
mode.

12
fread( ) function

• The fread() function is used to read bytes form the file.


• Syntax of fread() function
• fread( ptr, int size, int n, FILE *fp );
• The fread() function takes four arguments.
• ptr : ptr is the reference of an array or a structure where data will be
stored after reading.
• size : size is the total number of bytes to be read from file.
• n : n is number of times a record will be read.
• FILE* : FILE* is a file where the records will be read.

13
getw( ) and putw( )function

• putw(), getw() functions are file handling function in C programming


language which is used to write an integer value into a file (putw) and
read integer value from a file (getw).
• Declaration: int putw(int number, FILE *fp);
• putw function is used to write an integer into a file.
• putw(i, fp); where i is integer value and fp is file pointer.
• Declaration: int getw(FILE *fp);
• getw function reads an integer value from a file pointed by fp.
• getw(fp);
• Refer program putw_getw.c

14
Random File Operations

• IO is not confined to sequential motion through a file. May also shift


the file position back and forth to any specified location.
• Three functions:
• long ftell(FILE *fp);
• int fseek(FILE *fp, long offset, int from);
• void rewind(FILE *fp);

15
ftell( ) function

• Declaration: long int ftell(FILE *fp)


• ftell function is used to get current position of the file pointer. In a C
program, we use ftell() as below.
ftell(fp);
• Refer program ftell_rewind.c

16
rewind( ) function

• rewind function is used to move file pointer position to the beginning


of the file
• Declaration: void rewind(FILE *fp)
• In a C program, we use rewind() as below.
rewind(fp);
• Refer program ftell_rewind.c

17
fseek( ) function

• fseek() function is used to move file pointer position to the given


location.
• Declaration: int fseek(FILE *fp, long int offset, int whence)
• where, fp – file pointer
• offset – Number of bytes/characters to be offset/moved from
whence/the current file pointer position
• whence – This is the current file pointer position from where offset is
added. Below 3 constants are used to specify this.
• SEEK_SET – It moves file pointer position to the beginning of the file.
• SEEK_CUR – It moves file pointer position to given location.
• SEEK_END – It moves file pointer position to the end of file.
• Refer program fseek.c

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

You might also like