Mrs. G. Nagalakshmi, M.Sc., M.Phil.,
Ms. N. Malathi, M.Sc.,
Programming in C (18UAMA41)
II B.Sc. Mathematics (SF)
A file is a space in a memory where data is stored. 'C'
programming provides various functions to deal with a file. A
mechanism of manipulating with the files is called
as file management. A file must be opened before performing
operations on it. A file can be opened in a read, write or an append
mode.
Whenever we want to work with a file, the first step is to
create a file. A file is nothing but space in a memory where data is
stored.
To create a file in a 'C' program following syntax is used,
FILE *fp;
fp = fopen ("file_name", "mode");
fopen is a standard function which is used to open a file.
•If the file is not present on the system, then it is created and then
opened.
•If a file is already present on the system, then it is directly opened
using this function.
fp is a file pointer which points to the type file.
A file in 'C' programming can be created or opened for
reading/writing purposes. A mode is used to specify whether you
want to open a file for any of the below-given purposes. Following
are the different types of modes in 'C' programming which can be
used while working with a file.
File
Mode
Description
r Open a file for reading. If a file is in reading mode, then no
data is deleted if a file is already present on a system.
w Open a file for writing. If a file is in writing mode, then a new
file is created if a file doesn't exist at all. If a file is already
present on a system, then all the data inside the file is
truncated, and it is opened for writing purposes.
a Open a file in append mode. If a file is in append mode, then
the file is opened. The content within the file doesn't
change.
One should always close a file whenever the operations on file
are over. It means the contents and links to the file are terminated.
This prevents accidental damage to the file.
'C' provides the fclose function to perform file closing
operation. The syntax of fclose is as follows,
fclose (file_pointer);
There are three different functions dedicated to reading data from a
file
fgetc(file_pointer): It returns the next character from the file
pointed to by the file pointer. When the end of the file has been
reached, the EOF is sent back.
fgets(string, n, file_pointer): It reads n-1 characters from the file
and stores the string in which the NULL character '0' is appended as
the last character.
fscanf(file_pointer, conversion_specifiers, variable_adresses): It
is used to parse and analyze data. It reads characters from the file
and assigns the input to a list of variable pointers variable_adresses
using conversion specifiers. fscanf stops reading a string when space
or newline is encountered.
In C, when you write to a file, newline characters 'n' must be
explicitly added.
The stdio library offers the necessary functions to write to a file:
fputc(char, file_pointer): It writes a character to the file pointed to
by file_pointer.
fputs(str, file_pointer): It writes a string to the file pointed to by
file_pointer.
fprintf(file_pointer, str, variable_lists): It prints a string to the file
pointed to by file_pointer. The string can optionally include format
specifiers and a list of variables variable_lists.
While dealing with files, it is possible that an error may occur. This
error may occur due to following reasons:
•Reading beyond the end of file mark.
•Performing operations on the file that has not still been opened.
•Writing to a file that is opened in the read mode.
•Opening a file with invalid filename.
•Device overflow.
Thus to check the status of the pointer in the file and to detect the
error is the file. C provides two status-enquiry library functions
The feof() function can be used to test for an end of file
condition
Syntax
feof(FILE *file_pointer);
Example
if(feof(fp))
printf(“End of file”);
The ferror() function reports on the error state of the stream
and returns true if an error has occurred.
Syntax
ferror(FILE *file_pointer);
Example
if(ferror(fp)!=0)
printf(“An error has occurred”);
There is no need to read each record sequentially, if we want
to access a particular record. C supports these functions for random
access file processing.
•fseek()
•ftell()
•rewind()
This function is used for seeking the pointer position in the
file at the specified byte.
Syntax: fseek( file pointer, displacement, pointer position);
Where
file pointer ---- It is the pointer which points to the file.
displacement ---- It is positive or negative.This is the number of
bytes which are skipped backward (if negative) or forward( if
positive) from the current position.This is attached with L because
this is a long integer.
Examples:
1) fseek( p,10L,0)
0 means pointer position is on beginning of the file, from this
statement pointer position is skipped 10 bytes from the beginning of
the file.
2)fseek( p,5L,1)
1 means current position of the pointer position. From this statement
pointer position is skipped 5 bytes forward from the current position.
3)fseek(p,-5L,1)
From this statement pointer position is skipped 5 bytes backward
from the current position.
This function returns the value of the current pointer position in the
file. The value is count from the beginning of the file.
Syntax: ftell(fptr);
Where fptr is a file pointer.
This function is used to move the file pointer to the beginning of the
given file.
Syntax: rewind( fptr);
Where fptr is a file pointer.
Command line argument is a parameter supplied to the
program when it is invoked. Command line argument is an important
concept in C programming. It is mostly used when you need to
control your program from outside. Command line arguments are
passed to the main() method.
Syntax:
int main(int argc, char *argv[])
Here argc counts the number of arguments on the command line
and argv[ ] is a pointer array which holds pointers of type char which
points to the arguments passed to the program.
Programming in C

Programming in C

  • 1.
    Mrs. G. Nagalakshmi,M.Sc., M.Phil., Ms. N. Malathi, M.Sc., Programming in C (18UAMA41) II B.Sc. Mathematics (SF)
  • 3.
    A file isa space in a memory where data is stored. 'C' programming provides various functions to deal with a file. A mechanism of manipulating with the files is called as file management. A file must be opened before performing operations on it. A file can be opened in a read, write or an append mode.
  • 4.
    Whenever we wantto work with a file, the first step is to create a file. A file is nothing but space in a memory where data is stored. To create a file in a 'C' program following syntax is used, FILE *fp; fp = fopen ("file_name", "mode"); fopen is a standard function which is used to open a file. •If the file is not present on the system, then it is created and then opened. •If a file is already present on the system, then it is directly opened using this function. fp is a file pointer which points to the type file.
  • 5.
    A file in'C' programming can be created or opened for reading/writing purposes. A mode is used to specify whether you want to open a file for any of the below-given purposes. Following are the different types of modes in 'C' programming which can be used while working with a file.
  • 6.
    File Mode Description r Open afile for reading. If a file is in reading mode, then no data is deleted if a file is already present on a system. w Open a file for writing. If a file is in writing mode, then a new file is created if a file doesn't exist at all. If a file is already present on a system, then all the data inside the file is truncated, and it is opened for writing purposes. a Open a file in append mode. If a file is in append mode, then the file is opened. The content within the file doesn't change.
  • 7.
    One should alwaysclose a file whenever the operations on file are over. It means the contents and links to the file are terminated. This prevents accidental damage to the file. 'C' provides the fclose function to perform file closing operation. The syntax of fclose is as follows, fclose (file_pointer);
  • 8.
    There are threedifferent functions dedicated to reading data from a file fgetc(file_pointer): It returns the next character from the file pointed to by the file pointer. When the end of the file has been reached, the EOF is sent back. fgets(string, n, file_pointer): It reads n-1 characters from the file and stores the string in which the NULL character '0' is appended as the last character. fscanf(file_pointer, conversion_specifiers, variable_adresses): It is used to parse and analyze data. It reads characters from the file and assigns the input to a list of variable pointers variable_adresses using conversion specifiers. fscanf stops reading a string when space or newline is encountered.
  • 9.
    In C, whenyou write to a file, newline characters 'n' must be explicitly added. The stdio library offers the necessary functions to write to a file: fputc(char, file_pointer): It writes a character to the file pointed to by file_pointer. fputs(str, file_pointer): It writes a string to the file pointed to by file_pointer. fprintf(file_pointer, str, variable_lists): It prints a string to the file pointed to by file_pointer. The string can optionally include format specifiers and a list of variables variable_lists.
  • 10.
    While dealing withfiles, it is possible that an error may occur. This error may occur due to following reasons: •Reading beyond the end of file mark. •Performing operations on the file that has not still been opened. •Writing to a file that is opened in the read mode. •Opening a file with invalid filename. •Device overflow. Thus to check the status of the pointer in the file and to detect the error is the file. C provides two status-enquiry library functions
  • 11.
    The feof() functioncan be used to test for an end of file condition Syntax feof(FILE *file_pointer); Example if(feof(fp)) printf(“End of file”);
  • 12.
    The ferror() functionreports on the error state of the stream and returns true if an error has occurred. Syntax ferror(FILE *file_pointer); Example if(ferror(fp)!=0) printf(“An error has occurred”);
  • 13.
    There is noneed to read each record sequentially, if we want to access a particular record. C supports these functions for random access file processing. •fseek() •ftell() •rewind()
  • 14.
    This function isused for seeking the pointer position in the file at the specified byte. Syntax: fseek( file pointer, displacement, pointer position); Where file pointer ---- It is the pointer which points to the file. displacement ---- It is positive or negative.This is the number of bytes which are skipped backward (if negative) or forward( if positive) from the current position.This is attached with L because this is a long integer.
  • 15.
    Examples: 1) fseek( p,10L,0) 0means pointer position is on beginning of the file, from this statement pointer position is skipped 10 bytes from the beginning of the file. 2)fseek( p,5L,1) 1 means current position of the pointer position. From this statement pointer position is skipped 5 bytes forward from the current position. 3)fseek(p,-5L,1) From this statement pointer position is skipped 5 bytes backward from the current position.
  • 16.
    This function returnsthe value of the current pointer position in the file. The value is count from the beginning of the file. Syntax: ftell(fptr); Where fptr is a file pointer.
  • 17.
    This function isused to move the file pointer to the beginning of the given file. Syntax: rewind( fptr); Where fptr is a file pointer.
  • 18.
    Command line argumentis a parameter supplied to the program when it is invoked. Command line argument is an important concept in C programming. It is mostly used when you need to control your program from outside. Command line arguments are passed to the main() method. Syntax: int main(int argc, char *argv[]) Here argc counts the number of arguments on the command line and argv[ ] is a pointer array which holds pointers of type char which points to the arguments passed to the program.