SlideShare a Scribd company logo
1 of 19
Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
Typing Speed
Week

Target Achieved

1

40

23

2

40

25

3

40

25
Jobs Applied
Week
1
2
3

Company

Designation

Applied Date

Current Status
File Operations in c

Muhammed Noufal V T
muhammednoufalvt@gmail.com
www.facebook.com/vtnoufalvt
twitter.com/noufalurnappy
in.linkedin.com/pub/muhammed-noufal
9744003056
File management
• Data can be stored on the disk and read whenever necessary, without destroying
the data
• C uses a structure called FILE (defined in stdio.h) to store the attributes of a file.
• FILE- Place on the disk where a group of related data is stored
• C supports a number of functions that have the ability to perform basic file
operations
-Naming a file
-Opening a file
-Reading data from a file
-Writing data to a file
-Closing a file
Basic file operations
• C provides several different file operations
• fopen - open a file- specify how its opened (read/write) and type
(binary/text)
• fclose - close an opened file
• fread - read from a file
• fwrite - write to a file
• fseek/fsetpos - move a file pointer to somewhere in a file.
• ftell/fgetpos - tell you where the file pointer is located.
Different modes

• Writing mode
– if file already exists then contents are deleted,
– else new file with specified name created
• Appending mode
– if file already exists then file opened with contents safe
– else new file created
• Reading mode
– if file already exists then opened with contents safe
– else error occurs.
• Additional modes
– r+ open to beginning for both reading/writing
– w+ same as w except both for reading and writing
– a+ same as ‘a’ except both for reading and writing
Opening a FILE

• Syntax
-FILE *fp; /*variable fp is pointer to type FILE*/
fp = fopen(“filename”, “mode”); /*opens file with name filename , assigns
identifier to fp */
• fp
– contains all information about file
– Communication link between system and program
• The file mode tells C how the program will use the file.
• The filename indicates the system name and location for the file.
• We assign the return value of fopen to our pointer variable:
fp = fopen(“MYFILE.TXT”, “w”);
fp = fopen(“A:MYFILE.TXT”, “w”);
Closing a FILE

• When we finish with a mode, we need to close the file before ending the program or
beginning another mode with that same file.
• To close a file, we use fclose and the pointer variable:
fclose(spData);
• Example:
FILE *p1;
p1 = fopen(“INPUT.txt”, “r”);
……..
……..
fclose(p1);
• pointer can be reused after closing
Input/output operations on FILES
•
•
•
•
•
•
•

C provides several different functions for reading/writing
getc() – read a character
putc() – write a character
fprintf() – write set of data values
fscanf() – read set of data values
getw() – read integer
putw() – write integer
getc() & putc()
• handle one character at a time like getchar() and putchar()
• syntax: putc(c,fp1);
– c : a character variable
– fp1 : pointer to file opened with mode w
• syntax: c = getc(fp2);
– c : a character variable
– fp2 : pointer to file opened with mode r
• file pointer moves by one character position after every getc() and putc()
• getc() returns end-of-file marker EOF when file end reached
Program to read/write using getc() & putc()
•

#include <stdio.h>
main()
{
FILE *fp1;
char c;
f1= fopen(“INPUT”, “w”);
while((c=getchar()) != EOF)
putc(c,f1);
fclose(f1);
f1=fopen(“INPUT”, “r”);
while((c=getc(f1))!=EOF)
printf(“%c”, c);
fclose(f1);
} /*end main */

/* open file for writing */
/*get char from keyboard until CTL-Z*/
/*write a character to INPUT */
/* close INPUT */
/* reopen file */
/*read character from file INPUT*/
/* print character to screen */
fprintf() & fscanf
• similar to scanf() and printf()
• Syntax:- fprintf()
fprintf (fp,"string",variables);
Example:
int i = 12;
float x = 2.356;
char ch = 's';
FILE *fp;
fp=fopen(“out.txt”,”w”);
fprintf (fp, "%d %f %c", i, x, ch);

• Syntax:- fscanf()
fscanf (fp,"string",identifiers);
Example:
FILE *fp;
Fp=fopen(“input.txt”,”r”);
int i;
fscanf (fp,“%d",i);
• fscanf() returns EOF when end-of file
reached
getw() & putw()
• handle one integer at a time
• syntax: putw(i,fp1);

– i : an integer variable
– fp1 : pointer to file ipened with mode w

• syntax: i = getw(fp2);

– i : an integer variable
– fp2 : pointer to file opened with mode r

• file pointer moves by one integer position, data stored in binary format native
to local system
• getw() returns end-of-file marker EOF when file end reached
fread()
• Declaration:- fread(void *ptr, size, n, FILE *stream);
• Remarks:
fread reads a specified number of equal-sized data items from an input stream into a block.
– ptr = Points to a block into which data is read
– size = Length of each item read, in bytes
– n = Number of items read
– stream = file pointer
• Example
char a[10]={'1','2','3','4','5','6','7','8','9','a'};
FILE *fs;
fs=fopen("Project.txt","w");
fwrite(a,1,10,fs);
fclose(fs)
fwrite()
• Declaration:
fwrite(const void *ptr, size, n, FILE*stream);
• Remarks:
fwrite appends a specified number of equal-sized data items to an output file.
– ptr = Pointer to any object; the data written begins at ptr
– size = Length of each item of data
– n =Number of data items to be appended
– stream = file pointer
• Example
char a[10]={'1','2','3','4','5','6','7','8','9','a'};
FILE *fs;
fs=fopen("Project.txt","w");
fwrite(a,1,10,fs);
fclose(fs);
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.

Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550

Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com

NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550

More Related Content

What's hot (20)

Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Array in c++
Array in c++Array in c++
Array in c++
 
File operations
File operationsFile operations
File operations
 
File handling-c
File handling-cFile handling-c
File handling-c
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
C program
C programC program
C program
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
C functions
C functionsC functions
C functions
 
File in C language
File in C languageFile in C language
File in C language
 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Files in c++
Files in c++Files in c++
Files in c++
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 

Similar to Disclaimer and File Operations

Similar to Disclaimer and File Operations (20)

File management
File managementFile management
File management
 
1file handling
1file handling1file handling
1file handling
 
Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in C
 
C-Programming Chapter 5 File-handling-C.ppt
C-Programming Chapter 5 File-handling-C.pptC-Programming Chapter 5 File-handling-C.ppt
C-Programming Chapter 5 File-handling-C.ppt
 
Engineering Computers L34-L35-File Handling.pptx
Engineering Computers L34-L35-File Handling.pptxEngineering Computers L34-L35-File Handling.pptx
Engineering Computers L34-L35-File Handling.pptx
 
File Management
File ManagementFile Management
File Management
 
File management
File managementFile management
File management
 
Unit5
Unit5Unit5
Unit5
 
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
 
Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4
 
637225560972186380.pdf
637225560972186380.pdf637225560972186380.pdf
637225560972186380.pdf
 
File handling With Solve Programs
File handling With Solve ProgramsFile handling With Solve Programs
File handling With Solve Programs
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
File management
File managementFile management
File management
 
File mangement
File mangementFile mangement
File mangement
 
Unit 8
Unit 8Unit 8
Unit 8
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 

More from baabtra.com - No. 1 supplier of quality freshers

More from baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 

Recently uploaded

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
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
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 

Recently uploaded (20)

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
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
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 

Disclaimer and File Operations

  • 1.
  • 2. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 5. File Operations in c Muhammed Noufal V T muhammednoufalvt@gmail.com www.facebook.com/vtnoufalvt twitter.com/noufalurnappy in.linkedin.com/pub/muhammed-noufal 9744003056
  • 6. File management • Data can be stored on the disk and read whenever necessary, without destroying the data • C uses a structure called FILE (defined in stdio.h) to store the attributes of a file. • FILE- Place on the disk where a group of related data is stored • C supports a number of functions that have the ability to perform basic file operations -Naming a file -Opening a file -Reading data from a file -Writing data to a file -Closing a file
  • 7. Basic file operations • C provides several different file operations • fopen - open a file- specify how its opened (read/write) and type (binary/text) • fclose - close an opened file • fread - read from a file • fwrite - write to a file • fseek/fsetpos - move a file pointer to somewhere in a file. • ftell/fgetpos - tell you where the file pointer is located.
  • 8. Different modes • Writing mode – if file already exists then contents are deleted, – else new file with specified name created • Appending mode – if file already exists then file opened with contents safe – else new file created • Reading mode – if file already exists then opened with contents safe – else error occurs. • Additional modes – r+ open to beginning for both reading/writing – w+ same as w except both for reading and writing – a+ same as ‘a’ except both for reading and writing
  • 9. Opening a FILE • Syntax -FILE *fp; /*variable fp is pointer to type FILE*/ fp = fopen(“filename”, “mode”); /*opens file with name filename , assigns identifier to fp */ • fp – contains all information about file – Communication link between system and program • The file mode tells C how the program will use the file. • The filename indicates the system name and location for the file. • We assign the return value of fopen to our pointer variable: fp = fopen(“MYFILE.TXT”, “w”); fp = fopen(“A:MYFILE.TXT”, “w”);
  • 10. Closing a FILE • When we finish with a mode, we need to close the file before ending the program or beginning another mode with that same file. • To close a file, we use fclose and the pointer variable: fclose(spData); • Example: FILE *p1; p1 = fopen(“INPUT.txt”, “r”); …….. …….. fclose(p1); • pointer can be reused after closing
  • 11. Input/output operations on FILES • • • • • • • C provides several different functions for reading/writing getc() – read a character putc() – write a character fprintf() – write set of data values fscanf() – read set of data values getw() – read integer putw() – write integer
  • 12. getc() & putc() • handle one character at a time like getchar() and putchar() • syntax: putc(c,fp1); – c : a character variable – fp1 : pointer to file opened with mode w • syntax: c = getc(fp2); – c : a character variable – fp2 : pointer to file opened with mode r • file pointer moves by one character position after every getc() and putc() • getc() returns end-of-file marker EOF when file end reached
  • 13. Program to read/write using getc() & putc() • #include <stdio.h> main() { FILE *fp1; char c; f1= fopen(“INPUT”, “w”); while((c=getchar()) != EOF) putc(c,f1); fclose(f1); f1=fopen(“INPUT”, “r”); while((c=getc(f1))!=EOF) printf(“%c”, c); fclose(f1); } /*end main */ /* open file for writing */ /*get char from keyboard until CTL-Z*/ /*write a character to INPUT */ /* close INPUT */ /* reopen file */ /*read character from file INPUT*/ /* print character to screen */
  • 14. fprintf() & fscanf • similar to scanf() and printf() • Syntax:- fprintf() fprintf (fp,"string",variables); Example: int i = 12; float x = 2.356; char ch = 's'; FILE *fp; fp=fopen(“out.txt”,”w”); fprintf (fp, "%d %f %c", i, x, ch); • Syntax:- fscanf() fscanf (fp,"string",identifiers); Example: FILE *fp; Fp=fopen(“input.txt”,”r”); int i; fscanf (fp,“%d",i); • fscanf() returns EOF when end-of file reached
  • 15. getw() & putw() • handle one integer at a time • syntax: putw(i,fp1); – i : an integer variable – fp1 : pointer to file ipened with mode w • syntax: i = getw(fp2); – i : an integer variable – fp2 : pointer to file opened with mode r • file pointer moves by one integer position, data stored in binary format native to local system • getw() returns end-of-file marker EOF when file end reached
  • 16. fread() • Declaration:- fread(void *ptr, size, n, FILE *stream); • Remarks: fread reads a specified number of equal-sized data items from an input stream into a block. – ptr = Points to a block into which data is read – size = Length of each item read, in bytes – n = Number of items read – stream = file pointer • Example char a[10]={'1','2','3','4','5','6','7','8','9','a'}; FILE *fs; fs=fopen("Project.txt","w"); fwrite(a,1,10,fs); fclose(fs)
  • 17. fwrite() • Declaration: fwrite(const void *ptr, size, n, FILE*stream); • Remarks: fwrite appends a specified number of equal-sized data items to an output file. – ptr = Pointer to any object; the data written begins at ptr – size = Length of each item of data – n =Number of data items to be appended – stream = file pointer • Example char a[10]={'1','2','3','4','5','6','7','8','9','a'}; FILE *fs; fs=fopen("Project.txt","w"); fwrite(a,1,10,fs); fclose(fs);
  • 18. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 19. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550