File Handling & Command Line Arguments in C Programming: School of Computing and Information Technology
File Handling & Command Line Arguments in C Programming: School of Computing and Information Technology
F il e H a n d l i n g & C o m m a n d L i n e A r g u m e n t s
in C P ro g r a m m i n g
AY:2020-2021
FILES
Definition
FILE HANDLING
Introduction
AThe
large program
input outputin c can belike
functions divided toscanf(),
printf(), many subprogram
getchar(), putchar() etc are known as console
. oriented I/O functions which always use input devices and computer screen or monitor for
output devices. Using these library function, the entire data is lost when either the program is
terminated or the computer is turned off.
This problem invites concept of data files in which data can be stored on the disks and read
whenever necessary, without destroying data.
The data file allows us to store information and to access and alter the information whenever
necessary.
FILE HANDLING
Introduction
A large program in c can be divided to many subprogram
. C has various library functions for creating and processing data files
Mainly there are two types of data files, one is stream oriented or standard or high level and
another is system oriented or low level data files.
The standard data files are again subdivided into text files and binary files.
The text files consists of consecutive characters and these characters are interpreted as individual
data item.
The binary files organize data into blocks containing contiguous bytes of information.
For each binary and text files, there are number of formatted and unformatted library functions in
C.
FILE HANDLING
Introduction
A large program in c can be divided to many subprogram
.
OPENING AND CLOSING FILE
The ptr_variable is a pointer “pointer to the data type FILE” that stores the beginning address of
the buffer area allocated after a file has been opened.
This pointer contains all the information about the file and it is used as a communication link
between the system and the program.
ptr_variable=fopen(file_name, file_mode)
OPENING AND CLOSING FILE
large
A programfopen
The function in c can be divided
returns to to
a pointer many subprogram
the beginning of the buffer area associated with
. the file.
A NULL value is returned if the file can not be opened due to some reasons.
After opening a file, we can process data and finally we close it.
OPENING FILE
A largea program
Closing in that
file ensures c canallbe divided toinformation
outstanding many subprogram
associated with the file is flushed out from
the
. buffers.
Ex: fclose(ptr_variable);
This method returns zero if the stream is successfully closed. On failure, EOF is returned.
CLOSING FILE
A largea program
Closing in that
file ensures c canallbe divided toinformation
outstanding many subprogram
associated with the file is flushed out from
the
. buffers.
Ex: fclose(ptr_variable);
This method returns zero if the stream is successfully closed. On failure, EOF is returned.
LIBRARY FUNCTIONS FOR READING/WRITING FROM/TO A
FORMATTED INPUT/OUTPUT
FILE
• These functions are used to read numbers, characters or string from file or write them to file in
format as our requirement.
I. fprintf() This function is formatted output function which is used to write some integer, float,
char or string to a file.
Its syntax is fprintf(file_ptr_variable, ”control string”, list_variables);
The fprintf() function is used to write set of characters into file. It sends formatted output to a
stream.
II. fscanf() This function is formatted input function which is used to read some integer,float,
char or string from a file.
Its syntax is fscanf(file_ptr_variable, “control_string” ,&list_variables );
The fscanf() function is used to read character set i.e strings from the file. It returns the EOF,
when all the content of the file are read by it.
STRING INPUT/OUTPUT
AUsing
largestring
program in c can bedata
I/O functions, divided toread
can be many subprogram
from a file or written to a file in the form of array of
. characters.
I. fgets()
• It is used to read string from file.
Its syntax is
• fgets(string, int_value,file_ptr_variable); //int_value represents the number of character in
string
II. fputs()
• It is used to write a string to a file.
Its syntax is
• fputs(string, file_ptr_variable);
CHARACTER INPUT/OUTPUT
• AUsing
largethese
program in c can
functions, databecan
divided tofrom
be read manyfilesubprogram
or written to file one character at a time.
I. . fgetc() It is used to read a character from a file.
Its syntax is
char_variable=fgetc(file_ptr_variable);
The fgetc() function returns a single character from the file. It gets a character from the stream.
It returns EOF at the end of file
#include <stdio.h>
int main()
{
FILE * file;
if (file = fopen("Hello.txt", "w"))
{
#include <stdio.h>
int main()
{
FILE * fp;
char str[500];
float f;
fp=fopen("hello.txt","r");
fscanf(fp,"%f",&f);
fscanf(fp, "%s", str);
fclose(fp);
printf("contents are: %f and %s\n",f,str);
return 0;
}
fprintf()
#include <stdio.h>
int main()
{
FILE * fp;
char str[500];
float f;
fp=fopen("hello.txt","w");
fprintf(fp,"%s","REVA University");
fclose(fp);
return 0;
}
fgetc()
#include <stdio.h>
int main()
{
FILE * fp;
fp=fopen("hello.txt","r");
int ch=getc(fp);
while(ch!=EOF)
{
printf("%c",ch);
ch=getc(fp);
}
fclose(fp);
return 0;
}
fputc()
#include <stdio.h>
int main()
{
FILE * fp;
fp=fopen("hello.txt","w");
int ch=getc(fp);
for(ch=65;ch<=90;ch++)
{
putc(ch,fp);
}
fclose(fp);
return 0;
}
fgets()
#include <stdio.h>
int main()
{
char str[100];
FILE * fp;
fp=fopen("hello.txt","r");
fgets(str,100,fp);
printf("%s\n",str);
fclose(fp);
return 0;
}
fputs()
#include <stdio.h>
int main()
{
char str[100];
FILE * fp;
fp=fopen("hello.txt","w+");
fputs("Welcome to REVA university\n",fp);
fputs("Hello ABC",fp);
fclose(fp);
return 0;
}
DISCUSSION
5 MINUTES