SlideShare a Scribd company logo
1 of 15
1
Introduction to file management
2
• 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
Opening a file
3
• 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:cprogramprogram.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.
Opening a file
4
• 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.
closing a file
5
• 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);
Input/output operations on a file
6
Function
Name
Operation
fopen( ) Open the file for use
fgetc( ) Read a character from the file
fputc( ) Writes a character to the file
fclose( ) Close a file, which is open by file pointer
fprintf( ) Write a set of data values to files
fscanf( ) Read a set of data values from files.
fputs( ) Write string to file
fgets( ) Read string from file
fread( ) Read records (sequence of bytes) to the file.
fwrite( ) Write records (sequence of bytes) to the file.
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
10
• 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.
• Refer program fgets.c
fgets( ) and fputs( ) functions
11
• 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
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
Command line arguments
• Command line arguments are parameter or arguments passed to a
program when it runs from command prompt or invoked through
turboc environment.
• Example:C:tcbin > 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
14
15

More Related Content

What's hot

Management file and directory in linux
Management file and directory in linuxManagement file and directory in linux
Management file and directory in linuxZkre Saleh
 
4.6 create and change hard and symbolic links v2
4.6 create and change hard and symbolic links v24.6 create and change hard and symbolic links v2
4.6 create and change hard and symbolic links v2Acácio Oliveira
 
File management
File managementFile management
File managementsumathiv9
 
Productivity tips - Introduction to linux for bioinformatics
Productivity tips - Introduction to linux for bioinformaticsProductivity tips - Introduction to linux for bioinformatics
Productivity tips - Introduction to linux for bioinformaticsBITS
 
Text mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformaticsText mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformaticsBITS
 
Part 6 of "Introduction to linux for bioinformatics": Productivity tips
Part 6 of "Introduction to linux for bioinformatics": Productivity tipsPart 6 of "Introduction to linux for bioinformatics": Productivity tips
Part 6 of "Introduction to linux for bioinformatics": Productivity tipsJoachim Jacob
 
Unix command line concepts
Unix command line conceptsUnix command line concepts
Unix command line conceptsArtem Nagornyi
 
The unix file system
The unix file systemThe unix file system
The unix file systemgsandeepmenon
 
File management - ICT IGCSE Chapter 11
File management - ICT IGCSE Chapter 11File management - ICT IGCSE Chapter 11
File management - ICT IGCSE Chapter 11sayali patade
 
Managing your data - Introduction to Linux for bioinformatics
Managing your data - Introduction to Linux for bioinformaticsManaging your data - Introduction to Linux for bioinformatics
Managing your data - Introduction to Linux for bioinformaticsBITS
 

What's hot (20)

Management file and directory in linux
Management file and directory in linuxManagement file and directory in linux
Management file and directory in linux
 
4.6 create and change hard and symbolic links v2
4.6 create and change hard and symbolic links v24.6 create and change hard and symbolic links v2
4.6 create and change hard and symbolic links v2
 
Chapter 04
Chapter 04Chapter 04
Chapter 04
 
Basic linux day 3
Basic linux day 3Basic linux day 3
Basic linux day 3
 
Basic Linux day 1
Basic Linux day 1Basic Linux day 1
Basic Linux day 1
 
Basic Linux day 2
Basic Linux day 2Basic Linux day 2
Basic Linux day 2
 
File management
File managementFile management
File management
 
Productivity tips - Introduction to linux for bioinformatics
Productivity tips - Introduction to linux for bioinformaticsProductivity tips - Introduction to linux for bioinformatics
Productivity tips - Introduction to linux for bioinformatics
 
Text mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformaticsText mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformatics
 
Command
CommandCommand
Command
 
Basic Linux day 6
Basic Linux day 6Basic Linux day 6
Basic Linux day 6
 
Part 6 of "Introduction to linux for bioinformatics": Productivity tips
Part 6 of "Introduction to linux for bioinformatics": Productivity tipsPart 6 of "Introduction to linux for bioinformatics": Productivity tips
Part 6 of "Introduction to linux for bioinformatics": Productivity tips
 
Unix command line concepts
Unix command line conceptsUnix command line concepts
Unix command line concepts
 
File Management
File ManagementFile Management
File Management
 
The unix file system
The unix file systemThe unix file system
The unix file system
 
Basic linux day 4
Basic linux day 4Basic linux day 4
Basic linux day 4
 
File management - ICT IGCSE Chapter 11
File management - ICT IGCSE Chapter 11File management - ICT IGCSE Chapter 11
File management - ICT IGCSE Chapter 11
 
Basic unix commands_1
Basic unix commands_1Basic unix commands_1
Basic unix commands_1
 
Basic linux day 5
Basic linux day 5Basic linux day 5
Basic linux day 5
 
Managing your data - Introduction to Linux for bioinformatics
Managing your data - Introduction to Linux for bioinformaticsManaging your data - Introduction to Linux for bioinformatics
Managing your data - Introduction to Linux for bioinformatics
 

Similar to File management

Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4Prerna Sharma
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CAshim Lamichhane
 
Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O YourHelper1
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfsudhakargeruganti
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C ProgrammingRavindraSalunke3
 
File handling in c
File handling in cFile handling in c
File handling in caakanksha s
 
Understanding c file handling functions with examples
Understanding c file handling functions with examplesUnderstanding c file handling functions with examples
Understanding c file handling functions with examplesMuhammed Thanveer M
 
Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in CTushar B Kute
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5YOGESH SINGH
 

Similar to File management (20)

Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in C
 
637225560972186380.pdf
637225560972186380.pdf637225560972186380.pdf
637225560972186380.pdf
 
File_Management_in_C
File_Management_in_CFile_Management_in_C
File_Management_in_C
 
Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
 
File mangement
File mangementFile mangement
File mangement
 
File handling in c
File handling in cFile handling in c
File handling in c
 
File management
File managementFile management
File management
 
Unit 8
Unit 8Unit 8
Unit 8
 
Handout#01
Handout#01Handout#01
Handout#01
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
 
Understanding c file handling functions with examples
Understanding c file handling functions with examplesUnderstanding c file handling functions with examples
Understanding c file handling functions with examples
 
File Handling in C
File Handling in CFile Handling in C
File Handling in C
 
Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in C
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 

Recently uploaded

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Recently uploaded (20)

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 

File management

  • 1. 1
  • 2. Introduction to file management 2 • 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
  • 3. Opening a file 3 • 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:cprogramprogram.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.
  • 4. Opening a file 4 • 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.
  • 5. closing a file 5 • 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);
  • 6. Input/output operations on a file 6 Function Name Operation fopen( ) Open the file for use fgetc( ) Read a character from the file fputc( ) Writes a character to the file fclose( ) Close a file, which is open by file pointer fprintf( ) Write a set of data values to files fscanf( ) Read a set of data values from files. fputs( ) Write string to file fgets( ) Read string from file fread( ) Read records (sequence of bytes) to the file. fwrite( ) Write records (sequence of bytes) to the file.
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. fgets( ) and fputs( ) functions 10 • 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. • Refer program fgets.c
  • 11. fgets( ) and fputs( ) functions 11 • 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
  • 12. 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
  • 13. 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
  • 14. Command line arguments • Command line arguments are parameter or arguments passed to a program when it runs from command prompt or invoked through turboc environment. • Example:C:tcbin > 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 14
  • 15. 15