0% found this document useful (0 votes)
78 views

File Handling & Command Line Arguments in C Programming: School of Computing and Information Technology

The document discusses file handling in C programming, explaining that files allow programs to store and access data from disk. It covers opening, closing, reading from, and writing to text files using standard library functions like fopen(), fclose(), fprintf(), fscanf(), fgetc(), fputc(), fgets(), and fputs(). Examples are provided for each file handling function.

Uploaded by

MANOJKUMAR M
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

File Handling & Command Line Arguments in C Programming: School of Computing and Information Technology

The document discusses file handling in C programming, explaining that files allow programs to store and access data from disk. It covers opening, closing, reading from, and writing to text files using standard library functions like fopen(), fclose(), fprintf(), fscanf(), fgetc(), fputc(), fgets(), and fputs(). Examples are provided for each file handling function.

Uploaded by

MANOJKUMAR M
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Established as per the Section 2(f) of the UGC Act, 1956

Approved by AICTE, COA and BCI, New Delhi

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

School of Computing and Information


Te c h n o l o g y
Farooque Azam
[email protected]

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.

 A file is a place on the disk where a group of related data is stored.

 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

A large program in c can be divided to many subprogram


 . Before a program can write to a file or read from a file, the program must open it.
 Opening a file establishes a link between the program and the OS.
 This provides OS the name of the file and the mode in which the file is to be opened.
 While working with high level data file, we need buffer area where information is stored
temporarily in the course of transferring data between computer memory and data file.
 The buffer area is established as: FILE * ptr_variable;
OPENING AND CLOSING FILE

A large program in c can be divided to many subprogram


 . Here, FILE is a special structure, declared in header file stdio.h

 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.

 A data file is opened using syntax:

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 large program in c can be divided to many subprogram


.
OPENING FILE

A large program in c can be divided to many subprogram


.
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.
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

II. fputc()  It is used to write a character to a file.


Its syntax is
fputc(character or char_variable, file_ptr_variable);
The fputc() function is used to write a single character into file. It outputs a character to a
stream.
fopen()

#include <stdio.h>
int main()
{
FILE * file;
if (file = fopen("Hello.txt", "w"))
{

printf("File opened successfully in write mode");


}
else
printf("The file is not present! cannot create a new file using r mode");
fclose(file);
return 0;
}
fscanf()

#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

You might also like