SlideShare a Scribd company logo
1 of 8
Download to read offline
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 1
HANDOUT#01
Aim:
File Operation such as
Reading the file content
Writing the content to the file
Copying the content from one file to another file
Counting the number of character, words and lines of the file
Theory:
• A file represents a sequence of bytes on the disk where a group of related
data is stored. File is created for permanent storage of data. It is a readymade
structure.
• In C language, we use a structure pointer of file type to declare a file.
FILE *fp;
• C provides a number of functions that helps to perform basic file operations.
Following are the functions,
Function description
fopen() create a new file or open a existing file
fclose() closes a file
getc() reads a character from a file
putc() writes a character to a file
fscanf() reads a set of data from a file
fprintf() writes a set of data to a file
getw() reads a integer from a file
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 2
putw() writes a integer to a file
fseek() set the position to desire point
ftell() gives current position in the file
rewind() set the position to the begining point
• Opening a File or Creating a File
The fopen() function is used to create a new file or to open an existing file.
General Syntax :
*fp = FILE *fopen(const char *filename, const char *mode);
Here filename is the name of the file to be opened and mode specifies the
purpose of opening the file. Mode can be of following types,
*fp is the FILE pointer (FILE *fp), which will hold the reference to the
opened(or created) file.
mode description
r opens a text file in reading mode
w opens or create a text file in writing mode.
a opens a text file in append mode
r+ opens a text file in both reading and writing mode
w+ opens a text file in both reading and writing mode
a+ opens a text file in both reading and writing mode
rb opens a binary file in reading mode
wb opens or create a binary file in writing mode
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 3
ab opens a binary file in append mode
rb+ opens a binary file in both reading and writing mode
wb+ opens a binary file in both reading and writing mode
ab+ opens a binary file in both reading and writing mode
• Closing a File
The fclose() function is used to close an already opened file.
General Syntax :
int fclose( FILE *fp );
Here fclose() function closes the file and returns zero on success, or EOF if
there is an error in closing the file. This EOF is a constant defined in the
header file stdio.h.
• Writinga File
Following is the simplest function to write individual characters to a stream-
int fputc( int c, FILE *fp );
The function fputc() writes the character value of the argument c to the
output stream referenced by fp. It returns the written character written on
success otherwise EOF if there is an error. You can use the following
functions to write a null-terminated string to a stream –
int fputs( const char *s, FILE *fp );
The function fputs() writes the string s to the output stream referenced by fp.
It returns a non-negative value on success, otherwise EOF is returned in
case of any error. You can use int fprintf(FILE *fp,const char *format,
...)function as well to write a string into a file.
• Reading a File
Given below is the simplest function to read a single character from a file –
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 4
int fgetc( FILE * fp );
The fgetc() function reads a character from the input file referenced by fp.
The return value is the character read, or in case of any error, it returns EOF.
The following function allows to read a string from a stream –
char *fgets( char *buf, int n, FILE *fp );
The functions fgets() reads up to n-1 characters from the input stream
referenced by fp. It copies the read string into the buffer buf, appending
a null character to terminate the string.
If this function encounters a newline character 'n' or the end of the file EOF
before they have read the maximum number of characters, then it returns
only the characters read up to that point including the new line character.
You can also use int fscanf(FILE *fp, const char *format, ...) function to
read strings from a file, but it stops reading after encountering the first space
character.
[Reference: http://www.tutorialspoint.com/cprogramming/c_file_io.htm and
http://www.studytonight.com/c/file-input-output.php ]
Program:
1. Read the contents of file:
#include<stdio.h>
#include<stdlib.h>
void main(void)
{
FILE *fp;
char ch;
fp=fopen(“doc.txt”,”r”); //doc.txt is already existing file.
if(fp==NULL)
{
puts(“ can not open file”);
exit(1);
}
while(1)
{
ch=getch (fp);
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 5
if(ch=EOF)
{
break;
}
printf(“%c”,ch);
}
fclose(fp);
}
2. Writing into the file:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
FILE *fp;
char s[80];
fp=fopen(“poem.txt”,”w”);
if(fp==NULL)
{
puts(“Cannot open the file”);
exit(1);
}
printf(“nEnter a few lines of text:n”);
while(strlen(gets(s))>0)
{
fputs(s,fp);
fputs(“n”,fp);
}
fclose(fp);
}
3. Counting the no. of character, no. of blanks , no. of tabs, no. of lines:
#include<stdio.h>
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 6
void main()
{
FILE *fp;
char ch;
int nol=0 ,not=0 ,nob=0, noc=0;
fp=fopen(“file.txt”,”r”);
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
{
break;
}
noc++;
if(ch==’ ’)
{
nob++;
}
if(ch==’n’)
{
nol++;
}
if(ch==’t’)
{
not++;
}
}
fclose(fp);
printf(“nNumber of character: %d”,noc);
printf(“nNumber of blanks: %d”,nob);
printf(“nNumber of tabs: %d”,not);
printf(“nNumber of delete: %d”,nol);
}
4. Copy Program: writing the content of one file into another.
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 7
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fs ,*ft;
char ch;
fs=fopen(“poem.txt”,”r”); // poem.txt is already existing file.
if(fs==NULL)
{
puts(“Cannot open the file”);
exit(1);
}
ft=fopen(“new.txt”,”w”); /* new.txt is newly created file in which we
copy the content of poem.txt file. */
if(ft==NULL)
{
puts(“Cannot open the file”);
fclose(fs);
exit(1);
}
while(1)
{
ch=fgetc(fs);
if(ch==EOF)
{
break;
}
else
{
fputc(ch,ft);
}
}
fclose(fs);
fclose(ft);
}
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 8
Input:
Output:
Conclusion:
File Operation such as
Reading the file content
Writing the content to the file
Copying the content from one file to another file
Counting the number of character, words and lines of the file
are implemented in C-language.

More Related Content

What's hot

What's hot (20)

Handout#04
Handout#04Handout#04
Handout#04
 
Handout#09
Handout#09Handout#09
Handout#09
 
Handout#08
Handout#08Handout#08
Handout#08
 
Lexical analysis-using-lex
Lexical analysis-using-lexLexical analysis-using-lex
Lexical analysis-using-lex
 
Handout#12
Handout#12Handout#12
Handout#12
 
Compiler design and lexical analyser
Compiler design and lexical analyserCompiler design and lexical analyser
Compiler design and lexical analyser
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
7 compiler lab
7 compiler lab 7 compiler lab
7 compiler lab
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
 
Lex
LexLex
Lex
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
Data file handling
Data file handlingData file handling
Data file handling
 
Compiler Engineering Lab#1
Compiler Engineering Lab#1Compiler Engineering Lab#1
Compiler Engineering Lab#1
 
Lex and Yacc ppt
Lex and Yacc pptLex and Yacc ppt
Lex and Yacc ppt
 
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGESOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
 
Lexical Analyzers and Parsers
Lexical Analyzers and ParsersLexical Analyzers and Parsers
Lexical Analyzers and Parsers
 
Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?
 
C programming course material
C programming course materialC programming course material
C programming course material
 
C tutorial
C tutorialC tutorial
C tutorial
 
Assignment5
Assignment5Assignment5
Assignment5
 

Similar to Handout#01

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
 
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdfAdvance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdfsangeeta borde
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5YOGESH SINGH
 
File handling in c
File handling in cFile handling in c
File handling in caakanksha s
 
Files in C
Files in CFiles in C
Files in CPrabu U
 
File management
File managementFile management
File managementsumathiv9
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C ProgrammingRavindraSalunke3
 
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDYC UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDYRajeshkumar Reddy
 
ppt5-190810161800 (1).pdf
ppt5-190810161800 (1).pdfppt5-190810161800 (1).pdf
ppt5-190810161800 (1).pdfMalligaarjunanN
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10patcha535
 
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
 
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
 

Similar to Handout#01 (20)

4 text file
4 text file4 text file
4 text file
 
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
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdfAdvance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
 
File management
File managementFile management
File management
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
Lk module4 file
Lk module4 fileLk module4 file
Lk module4 file
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Files in C
Files in CFiles in C
Files in C
 
File management
File managementFile management
File management
 
Unit5
Unit5Unit5
Unit5
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
 
File handling C program
File handling C programFile handling C program
File handling C program
 
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDYC UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
 
ppt5-190810161800 (1).pdf
ppt5-190810161800 (1).pdfppt5-190810161800 (1).pdf
ppt5-190810161800 (1).pdf
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
 
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
 
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
 

More from Sunita Milind Dol (19)

9.Joins.pdf
9.Joins.pdf9.Joins.pdf
9.Joins.pdf
 
8.Views.pdf
8.Views.pdf8.Views.pdf
8.Views.pdf
 
7. Nested Subqueries.pdf
7. Nested Subqueries.pdf7. Nested Subqueries.pdf
7. Nested Subqueries.pdf
 
6. Aggregate Functions.pdf
6. Aggregate Functions.pdf6. Aggregate Functions.pdf
6. Aggregate Functions.pdf
 
5. Basic Structure of SQL Queries.pdf
5. Basic Structure of SQL Queries.pdf5. Basic Structure of SQL Queries.pdf
5. Basic Structure of SQL Queries.pdf
 
4. DML.pdf
4. DML.pdf4. DML.pdf
4. DML.pdf
 
3. DDL.pdf
3. DDL.pdf3. DDL.pdf
3. DDL.pdf
 
2. SQL Introduction.pdf
2. SQL Introduction.pdf2. SQL Introduction.pdf
2. SQL Introduction.pdf
 
1. University Example.pdf
1. University Example.pdf1. University Example.pdf
1. University Example.pdf
 
Assignment12
Assignment12Assignment12
Assignment12
 
Assignment10
Assignment10Assignment10
Assignment10
 
Assignment9
Assignment9Assignment9
Assignment9
 
Assignment8
Assignment8Assignment8
Assignment8
 
Assignment7
Assignment7Assignment7
Assignment7
 
Assignment6
Assignment6Assignment6
Assignment6
 
Assignment3
Assignment3Assignment3
Assignment3
 
Assignment2
Assignment2Assignment2
Assignment2
 
Handout#07
Handout#07Handout#07
Handout#07
 
Handout#06
Handout#06Handout#06
Handout#06
 

Recently uploaded

INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 

Recently uploaded (20)

INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 

Handout#01

  • 1. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 1 HANDOUT#01 Aim: File Operation such as Reading the file content Writing the content to the file Copying the content from one file to another file Counting the number of character, words and lines of the file Theory: • A file represents a sequence of bytes on the disk where a group of related data is stored. File is created for permanent storage of data. It is a readymade structure. • In C language, we use a structure pointer of file type to declare a file. FILE *fp; • C provides a number of functions that helps to perform basic file operations. Following are the functions, Function description fopen() create a new file or open a existing file fclose() closes a file getc() reads a character from a file putc() writes a character to a file fscanf() reads a set of data from a file fprintf() writes a set of data to a file getw() reads a integer from a file
  • 2. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 2 putw() writes a integer to a file fseek() set the position to desire point ftell() gives current position in the file rewind() set the position to the begining point • Opening a File or Creating a File The fopen() function is used to create a new file or to open an existing file. General Syntax : *fp = FILE *fopen(const char *filename, const char *mode); Here filename is the name of the file to be opened and mode specifies the purpose of opening the file. Mode can be of following types, *fp is the FILE pointer (FILE *fp), which will hold the reference to the opened(or created) file. mode description r opens a text file in reading mode w opens or create a text file in writing mode. a opens a text file in append mode r+ opens a text file in both reading and writing mode w+ opens a text file in both reading and writing mode a+ opens a text file in both reading and writing mode rb opens a binary file in reading mode wb opens or create a binary file in writing mode
  • 3. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 3 ab opens a binary file in append mode rb+ opens a binary file in both reading and writing mode wb+ opens a binary file in both reading and writing mode ab+ opens a binary file in both reading and writing mode • Closing a File The fclose() function is used to close an already opened file. General Syntax : int fclose( FILE *fp ); Here fclose() function closes the file and returns zero on success, or EOF if there is an error in closing the file. This EOF is a constant defined in the header file stdio.h. • Writinga File Following is the simplest function to write individual characters to a stream- int fputc( int c, FILE *fp ); The function fputc() writes the character value of the argument c to the output stream referenced by fp. It returns the written character written on success otherwise EOF if there is an error. You can use the following functions to write a null-terminated string to a stream – int fputs( const char *s, FILE *fp ); The function fputs() writes the string s to the output stream referenced by fp. It returns a non-negative value on success, otherwise EOF is returned in case of any error. You can use int fprintf(FILE *fp,const char *format, ...)function as well to write a string into a file. • Reading a File Given below is the simplest function to read a single character from a file –
  • 4. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 4 int fgetc( FILE * fp ); The fgetc() function reads a character from the input file referenced by fp. The return value is the character read, or in case of any error, it returns EOF. The following function allows to read a string from a stream – char *fgets( char *buf, int n, FILE *fp ); The functions fgets() reads up to n-1 characters from the input stream referenced by fp. It copies the read string into the buffer buf, appending a null character to terminate the string. If this function encounters a newline character 'n' or the end of the file EOF before they have read the maximum number of characters, then it returns only the characters read up to that point including the new line character. You can also use int fscanf(FILE *fp, const char *format, ...) function to read strings from a file, but it stops reading after encountering the first space character. [Reference: http://www.tutorialspoint.com/cprogramming/c_file_io.htm and http://www.studytonight.com/c/file-input-output.php ] Program: 1. Read the contents of file: #include<stdio.h> #include<stdlib.h> void main(void) { FILE *fp; char ch; fp=fopen(“doc.txt”,”r”); //doc.txt is already existing file. if(fp==NULL) { puts(“ can not open file”); exit(1); } while(1) { ch=getch (fp);
  • 5. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 5 if(ch=EOF) { break; } printf(“%c”,ch); } fclose(fp); } 2. Writing into the file: #include<stdio.h> #include<stdlib.h> #include<string.h> void main() { FILE *fp; char s[80]; fp=fopen(“poem.txt”,”w”); if(fp==NULL) { puts(“Cannot open the file”); exit(1); } printf(“nEnter a few lines of text:n”); while(strlen(gets(s))>0) { fputs(s,fp); fputs(“n”,fp); } fclose(fp); } 3. Counting the no. of character, no. of blanks , no. of tabs, no. of lines: #include<stdio.h>
  • 6. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 6 void main() { FILE *fp; char ch; int nol=0 ,not=0 ,nob=0, noc=0; fp=fopen(“file.txt”,”r”); while(1) { ch=fgetc(fp); if(ch==EOF) { break; } noc++; if(ch==’ ’) { nob++; } if(ch==’n’) { nol++; } if(ch==’t’) { not++; } } fclose(fp); printf(“nNumber of character: %d”,noc); printf(“nNumber of blanks: %d”,nob); printf(“nNumber of tabs: %d”,not); printf(“nNumber of delete: %d”,nol); } 4. Copy Program: writing the content of one file into another.
  • 7. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 7 #include<stdio.h> #include<stdlib.h> void main() { FILE *fs ,*ft; char ch; fs=fopen(“poem.txt”,”r”); // poem.txt is already existing file. if(fs==NULL) { puts(“Cannot open the file”); exit(1); } ft=fopen(“new.txt”,”w”); /* new.txt is newly created file in which we copy the content of poem.txt file. */ if(ft==NULL) { puts(“Cannot open the file”); fclose(fs); exit(1); } while(1) { ch=fgetc(fs); if(ch==EOF) { break; } else { fputc(ch,ft); } } fclose(fs); fclose(ft); }
  • 8. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 8 Input: Output: Conclusion: File Operation such as Reading the file content Writing the content to the file Copying the content from one file to another file Counting the number of character, words and lines of the file are implemented in C-language.