SlideShare a Scribd company logo
File I/O
2
File Input/Output
 Input/Output device for programs
– Input: Keyboard
– Output: Monitor
 Is it possible to read or write data from/to files?
A B C
1 80 90 70
2 80 60 40
3 60 50 70
…
C program
A B C Avg.
1 80 90 70 80
2 80 60 40 60
3 60 50 70 60
…
read write
file1.txt file2.txt
3
File Input/Ooutput
 File Input/Output Process
Open the file
Read data from file
Write data to file
Close the file
fopen
fscanf, fprintf, fgets, fputs, …
fclose
4
File Operation Function fopen()
 fopen()
– Used to prepare external files to use in programs
• All files are associated with a data structure known as a stream
if fopen successes
– Return value: a pointer of FILE structure
• File stream is manipulated with FILE pointer returned by fopen
 fopen() Syntax
FILE *fopen( const char *filename, const char *mode );
Return by file pointer Filename in disk File open mode
5
File Operation Function fopen()
 Example
– Open my_file.txt in the read mode
– Return NULL at failure
– Eg) If a file named “my_file.txt” does not exist
FILE *fp ;
fp = fopen(“my_file.txt”, “r”);
6
File Operation Function fopen()
 Example
– Open my_file.txt in the write mode
– If my_file.txt does not exist, it is created
– If my_file.txt exists, the content of the file is erased
FILE *fp ;
fp = fopen(“my_file.txt”, “w”);
7
File Operation Function fopen()
 Example
– Open my_file.txt in the append mode (similar to write mode)
– If my_file.txt does not exist, it is created
– If my_file.txt exists, the file content is kept and new data is
appended at the end of file
FILE *fp ;
fp = fopen(“my_file.txt”, “a”);
8
File Operation Function fopen()
 Example
– Open my_file.txt in the read/write mode
– Return NULL, if my_file.txt does not exist
– If my_file.txt exists, the file content is kept
– Read/write pointer is positioned at the beginning of the file
FILE *fp ;
fp = fopen(“my_file.txt”, “r+”);
9
File Operation Function fopen()
 Example
– Open my_file.txt in the read/write mode
– If my_file.txt does not exist, it is created
– If my_file.txt exists, the file content is erased
FILE *fp ;
fp = fopen(“my_file.txt”, “w+”);
10
File Operation Function fopen()
 Example
– Open my_file.txt in the read/write mode
– If my_file.txt does not exist, it is created
– If my_file.txt exists, the file content is kept
– Read/write pointer is positioned at the end of the file
FILE *fp ;
fp = fopen(“my_file.txt”, “a+”);
11
File pointer
 File pointer
– File I/O is processed through file pointer
 ‘FILE’ Type
 a type of structure defined with typedef in stdio.h contains
information of file
[Ex]
FILE *fp; /* declare ‘fp’ as
file pointer */
12
File Operation Function fclose()
 fclose()
– Close the file opened by fopen
 fclose() Syntax
int fclose( FILE *stream );
file pointer returned by fopen
Returns 0 : successful
EOF : error
(EOF is defined as –1 in stdio.h)
13
File Operation Function fopen()
 fclose() Example
#include <stdio.h>
void main() {
FILE *fp ;
fp = fopen(“my_file.txt”, “r”);
...
fclose( fp ) ;
}
14
Formatted I/O Function fprintf()
 fprintf()
– printf() for file operation
– First argument is the file pointer which has one of write
modes (w, a, r+, w+, a+)
– The rest arguments are the same as printf()
– fprintf() writes data into abc.txt
FILE* fp = fopen( “abc.txt”, “w” ) ;
fprintf( fp, “%d %dn”, 1, 2 );
15
Formatted I/O Function fprintf()
 fscanf()
– scanf() for file operation
– First argument is file pointer which has one of read modes
(r, r+, w+, a+)
– The rest arguments are the same as printf()
– scanf() reads data from abc.txt
int i, j ;
FILE* fp = fopen( “abc.txt”, “r” ) ;
fscanf( fp, “%d %d”, &i, &j );
16
feof() Function
 feof()
– Check whether the file pointer is at the end of file or not
int feof( FILE *stream );
Opened file pointer
Returns none-zero : if it is EOF
0 : if it is not EOF
17
Formatted I/O Function fprintf() and fscanf()
#include <stdio.h>
void main() {
int i, j, k ;
FILE *ifp, *ofp ;
ifp = fopen("abc.txt", "r" ) ;
ofp = fopen("cal.txt", "w" ) ;
fscanf( ifp, "%d %d %d", &i, &j, &k ) ;
fprintf( ofp, "sum: %dn", i + j + k ) ;
fclose( ifp ) ;
fclose( ofp ) ;
}
 fprintf(), fscanf() Example
1 2 3
abc.txt
cal.txt is not exist
18
Formatted I/O Function fprintf() and
fscanf()
#include <stdio.h>
void main() {
int i, j, k ;
FILE *ifp, *ofp ;
ifp = fopen("abc.txt", "r" ) ;
ofp = fopen("cal.txt", "w" ) ;
fscanf( ifp, "%d %d %d", &i, &j, &k ) ;
fprintf( ofp, "sum: %dn", i + j + k ) ;
fclose( ifp ) ;
fclose( ofp ) ;
}
 fprintf(), fscanf() Example
1 2 3
abc.txt
sum: 6
cal.txt
19
Formatted I/O Function fprintf() and
fscanf()
#include <stdio.h>
void main() {
int i, j, k ;
FILE *ifp, *ofp ;
ifp = fopen("abc.txt", "r" ) ;
ofp = fopen("cal.txt", "a" ) ;
fscanf( ifp, "%d %d %d", &i, &j, &k ) ;
fprintf( ofp, "sum: %dn", i + j + k ) ;
fclose( ifp ) ;
fclose( ofp ) ;
}
 fprintf(), fscanf() Example
1 2 3
abc.txt
sum: 6
cal.txt
20
Formatted I/O Function fprintf() and
fscanf()
#include <stdio.h>
void main() {
char c ;
FILE *ifp, *ofp ;
ifp = fopen("abc.txt", "r" ) ;
ofp = fopen("abc2.txt", "a" ) ;
while( feof(ifp) == 0 ) {
fscanf( ifp, "%c", &c ) ;
fprintf( ofp, "%c", c ) ;
}
fclose( ifp ) ;
fclose( ofp ) ;
}
 fprintf(), fscanf() Example
This is a
file.
1 2 3
abc.txt
This is a
file.
1 2 3
abc2.txt
21
Misc. File I/O Functions
 fgetc() , getc()
– Read a character from a file
• If there is no more character to read, return EOF
– fgetc() is equivalent to getc()
char c ;
FILE *fp = fopen("abc.txt", "r" ) ;
while( (c = fgetc(fp)) != EOF )
printf("%c", c ) ;
int fgetc ( FILE *stream ) ;
22
Misc. File I/O Functions
 fputc() , putc()
– Write a character to a file
– fputc() is identical to putc()
char c ;
FILE *fp = fopen("abc.txt", "r" ) ;
FILE *ofp = fopen("xyz.txt", "w" ) ;
while( (c = fgetc(fp)) != EOF )
fputc( c, ofp ) ;
int fputc ( int c, FILE *stream ) ;
23
Misc. File I/O Functions
 fputs() : Writing a string
– puts() for file I/O
– Print a string into file
– At success, it returns a non-negative value
– At failure, it returns EOF
int fputs(char *str, FILE *fp);
24
Misc. File I/O Functions
FILE *fp;
int i;
char *data[3]={"to ben","or notn","to ben"};
fp = fopen("abc.txt", "w");
for(i = 0; i<3; i++) fputs(data[i],fp);
fclose( fp );
25
Misc. File I/O Functions
 fgets() : Reading a String
– gets() for file I/O
– Read (num -1) characters from fp and store to str
– When fp meets ‘n’ or EOF, it stops reading and store NULL
into str
– At success, it returns the address of str. Otherwise, it
returns NULL
char *fgets(char *str, int num, FILE *fp);
26
Misc. File I/O Functions
 fgets()
char s[5] ;
FILE* fp = fopen("abc.txt", "r" ) ;
fgets( s, 5, fp ) ;
123
1 2
1234567
27
Misc. File I/O Functions
 Difference between gets and fgets
– gets: read before ‘n’. Not store ‘n’
– fgets: read before ‘n’. Store also ‘n’
28
Accessing a File Randomly
 How to read the file again?
char c ;
FILE *fp = fopen("abc.txt", "r" ) ;
while( !foef(fp) ) {
fscanf( fp, "%c", &c ) ;
printf("%c", c ) ;
}
while( !foef(fp) ) {
fscanf( fp, "%c", &c ) ;
printf("%c", c ) ;
}
abcd
efghi
abc.txt
29
Accessing a File Randomly
 rewind()
– Move file position indicator
to the beginning of the file
void rewind( FILE* ) ;
char c ;
FILE *fp = fopen("abc.txt", “r” ) ;
while( !foef(fp) ) {
fscanf( fp, "%c", &c ) ;
printf("%c", c ) ;
}
rewind( fp ) ;
while( !foef(fp) ) {
fscanf( fp, "%c", &c ) ;
printf("%c", c ) ;
}
abcd
efghi
abc.txt
30
Accessing a File Randomly
 fseek()
– Move file position indicator to offset from place
– Value of place
0 (starting point of the file)
1 (current position)
2 (end of the file)
fseek( file_ptr, offset, place);
31
Accessing a File Randomly
 fseek()
fseek( file_ptr, offset, place);
0 (SEEK_SET)
1 (SEEK_CUR)
2 (SEEK_END)
0123456789
abcdefghjk
abc.txt
FILE *fp = fopen("abc.txt", “r” ) ;
fseek(fp, 3, SEEK_SET) ;
fseek(fp, 5, SEEK_CUR) ;
fseek(fp, -10, SEEK_END) ;
32
Accessing a File Randomly
 ftell()
– Return the current value of file position indicator
– Returned value is the number of bytes from the beginning of
the file
– Value of indicator is increased by one when it read a
character
int ftell( FILE* );
33
Accessing a File Randomly
 ftell()
char c ;
int pos ;
FILE *fp = fopen("abc.txt", “r” ) ;
while( !foef(fp) ) {
fscanf( fp, "%c", &c ) ;
printf("%d: %cn", ftell(fp), c ) ;
}
abcd
efghi
abc.txt
34
stdin, stdout & stderr
 Print output with fprintf
– stdout is a file pointer for monitor
 Read from keyboard with fscanf
– Stdin is a file pointer for keyboard
printf( “This is a testn” ) ; fprintf( stdout, “This is a testn” ) ;
scanf( “%d”, &k ) ; fscanf( stdin, “%d”, &k ) ;
35
stdin, stdout & stderr
Standard C files in stdio.h
Written in C Name Remark
stdin standard input file connected to the keyboard
stdout standard output file connected to the screen
stderr standard error file connected to the screen
 Three kinds of file pointer in stdio.h
36
stdin, stdout & stderr
#include <stdio.h>
void main() {
int k, j ;
fscanf( stdin, "%d %d", &k, &j ) ;
fprintf( stdout, "Sum: %dn", k + j ) ;
fprintf( stderr, "OKn") ;
}

More Related Content

DOC
Unit v
PPTX
File handling in c
PDF
Module 03 File Handling in C
PPSX
C programming file handling
PPT
Unit5
PPT
File handling in c
PPTX
File handling in C
PPT
File handling-dutt
Unit v
File handling in c
Module 03 File Handling in C
C programming file handling
Unit5
File handling in c
File handling in C
File handling-dutt

What's hot (20)

PPTX
File management
DOCX
Satz1
PPT
File handling-c programming language
PPT
File handling in 'C'
PPT
file handling1
PPT
File in c
PPTX
File Management in C
PPTX
File in C language
DOCX
Understanding c file handling functions with examples
PPT
File Management
PDF
Module 5 file cp
PPTX
File handling in C
PPT
File handling in c
PPT
Lecture 20 - File Handling
DOCX
ODP
C 檔案輸入與輸出
DOCX
Unit 5 dwqb ans
PPSX
1file handling
PPT
File handling
File management
Satz1
File handling-c programming language
File handling in 'C'
file handling1
File in c
File Management in C
File in C language
Understanding c file handling functions with examples
File Management
Module 5 file cp
File handling in C
File handling in c
Lecture 20 - File Handling
C 檔案輸入與輸出
Unit 5 dwqb ans
1file handling
File handling
Ad

Viewers also liked (19)

PDF
15 1. enumeration
PDF
4. loop
PDF
5 2. string processing
PDF
6 function
PDF
14. fiile io
PDF
12 1. const pointer, typedef
PDF
3 2. if statement
PDF
10. pointer & function
PDF
2 2. operators
PDF
15 2. arguement passing to main
PDF
Goorm class
PDF
5 1. character processing
PDF
skku cp2 w4
PDF
6. functions
PDF
3 1. preprocessor, math, stdlib
PDF
2 1. variables & data types
PDF
W14 chap13
PDF
10. array & pointer
PDF
Cp2 w5
15 1. enumeration
4. loop
5 2. string processing
6 function
14. fiile io
12 1. const pointer, typedef
3 2. if statement
10. pointer & function
2 2. operators
15 2. arguement passing to main
Goorm class
5 1. character processing
skku cp2 w4
6. functions
3 1. preprocessor, math, stdlib
2 1. variables & data types
W14 chap13
10. array & pointer
Cp2 w5
Ad

Similar to 14. fiile io (20)

PPTX
want to learn files,then just use this ppt to learn
PPT
PDF
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
PPTX
File Handling ppt.pptx shjd dbkd z bdjdb d
PPTX
File handling in c
PPT
File handling(some slides only)
PPTX
Programming C- File Handling , File Operation
PDF
File Handling in C Programming
PPT
C-Programming Chapter 5 File-handling-C.ppt
PPT
file_handling_in_c.ppt
PPT
Unit5 C
PPTX
C Programming Unit-5
PPTX
PPS PPT 2.pptx
PDF
Files in C
PPT
Files_in_C.ppt
PPT
File Handling in c.ppt
PPT
File management and handling by prabhakar
PPS
C programming session 08
want to learn files,then just use this ppt to learn
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
File Handling ppt.pptx shjd dbkd z bdjdb d
File handling in c
File handling(some slides only)
Programming C- File Handling , File Operation
File Handling in C Programming
C-Programming Chapter 5 File-handling-C.ppt
file_handling_in_c.ppt
Unit5 C
C Programming Unit-5
PPS PPT 2.pptx
Files in C
Files_in_C.ppt
File Handling in c.ppt
File management and handling by prabhakar
C programming session 08

More from 웅식 전 (20)

PDF
15 3. modulization
PDF
15 2. arguement passing to main
PDF
13. structure
PDF
12 2. dynamic allocation
PDF
12 1. multi-dimensional array
PDF
11. array & pointer
PDF
9. pointer
PDF
7. variable scope rule,-storage_class
PDF
6. function
PDF
5 1. character processing
PDF
15 1. enumeration, typedef
PDF
4. loop
PDF
2 3. standard io
PDF
2 2. operators
PDF
2 1. variables & data types
PDF
Goorm ide 교육용버전 for skku(학생)
PDF
구름 기본 소개자료
PDF
Goorm ide 소개 슬라이드(교육용 버전)
PDF
13th chapter12 slide
PDF
Week12 chapter11
15 3. modulization
15 2. arguement passing to main
13. structure
12 2. dynamic allocation
12 1. multi-dimensional array
11. array & pointer
9. pointer
7. variable scope rule,-storage_class
6. function
5 1. character processing
15 1. enumeration, typedef
4. loop
2 3. standard io
2 2. operators
2 1. variables & data types
Goorm ide 교육용버전 for skku(학생)
구름 기본 소개자료
Goorm ide 소개 슬라이드(교육용 버전)
13th chapter12 slide
Week12 chapter11

Recently uploaded (20)

PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
cuic standard and advanced reporting.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
A Presentation on Artificial Intelligence
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
cuic standard and advanced reporting.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Review of recent advances in non-invasive hemoglobin estimation
Advanced methodologies resolving dimensionality complications for autism neur...
A Presentation on Artificial Intelligence
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Mobile App Security Testing_ A Comprehensive Guide.pdf
Electronic commerce courselecture one. Pdf
Machine learning based COVID-19 study performance prediction
Empathic Computing: Creating Shared Understanding
Understanding_Digital_Forensics_Presentation.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
NewMind AI Monthly Chronicles - July 2025
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Big Data Technologies - Introduction.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
The AUB Centre for AI in Media Proposal.docx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication

14. fiile io

  • 2. 2 File Input/Output  Input/Output device for programs – Input: Keyboard – Output: Monitor  Is it possible to read or write data from/to files? A B C 1 80 90 70 2 80 60 40 3 60 50 70 … C program A B C Avg. 1 80 90 70 80 2 80 60 40 60 3 60 50 70 60 … read write file1.txt file2.txt
  • 3. 3 File Input/Ooutput  File Input/Output Process Open the file Read data from file Write data to file Close the file fopen fscanf, fprintf, fgets, fputs, … fclose
  • 4. 4 File Operation Function fopen()  fopen() – Used to prepare external files to use in programs • All files are associated with a data structure known as a stream if fopen successes – Return value: a pointer of FILE structure • File stream is manipulated with FILE pointer returned by fopen  fopen() Syntax FILE *fopen( const char *filename, const char *mode ); Return by file pointer Filename in disk File open mode
  • 5. 5 File Operation Function fopen()  Example – Open my_file.txt in the read mode – Return NULL at failure – Eg) If a file named “my_file.txt” does not exist FILE *fp ; fp = fopen(“my_file.txt”, “r”);
  • 6. 6 File Operation Function fopen()  Example – Open my_file.txt in the write mode – If my_file.txt does not exist, it is created – If my_file.txt exists, the content of the file is erased FILE *fp ; fp = fopen(“my_file.txt”, “w”);
  • 7. 7 File Operation Function fopen()  Example – Open my_file.txt in the append mode (similar to write mode) – If my_file.txt does not exist, it is created – If my_file.txt exists, the file content is kept and new data is appended at the end of file FILE *fp ; fp = fopen(“my_file.txt”, “a”);
  • 8. 8 File Operation Function fopen()  Example – Open my_file.txt in the read/write mode – Return NULL, if my_file.txt does not exist – If my_file.txt exists, the file content is kept – Read/write pointer is positioned at the beginning of the file FILE *fp ; fp = fopen(“my_file.txt”, “r+”);
  • 9. 9 File Operation Function fopen()  Example – Open my_file.txt in the read/write mode – If my_file.txt does not exist, it is created – If my_file.txt exists, the file content is erased FILE *fp ; fp = fopen(“my_file.txt”, “w+”);
  • 10. 10 File Operation Function fopen()  Example – Open my_file.txt in the read/write mode – If my_file.txt does not exist, it is created – If my_file.txt exists, the file content is kept – Read/write pointer is positioned at the end of the file FILE *fp ; fp = fopen(“my_file.txt”, “a+”);
  • 11. 11 File pointer  File pointer – File I/O is processed through file pointer  ‘FILE’ Type  a type of structure defined with typedef in stdio.h contains information of file [Ex] FILE *fp; /* declare ‘fp’ as file pointer */
  • 12. 12 File Operation Function fclose()  fclose() – Close the file opened by fopen  fclose() Syntax int fclose( FILE *stream ); file pointer returned by fopen Returns 0 : successful EOF : error (EOF is defined as –1 in stdio.h)
  • 13. 13 File Operation Function fopen()  fclose() Example #include <stdio.h> void main() { FILE *fp ; fp = fopen(“my_file.txt”, “r”); ... fclose( fp ) ; }
  • 14. 14 Formatted I/O Function fprintf()  fprintf() – printf() for file operation – First argument is the file pointer which has one of write modes (w, a, r+, w+, a+) – The rest arguments are the same as printf() – fprintf() writes data into abc.txt FILE* fp = fopen( “abc.txt”, “w” ) ; fprintf( fp, “%d %dn”, 1, 2 );
  • 15. 15 Formatted I/O Function fprintf()  fscanf() – scanf() for file operation – First argument is file pointer which has one of read modes (r, r+, w+, a+) – The rest arguments are the same as printf() – scanf() reads data from abc.txt int i, j ; FILE* fp = fopen( “abc.txt”, “r” ) ; fscanf( fp, “%d %d”, &i, &j );
  • 16. 16 feof() Function  feof() – Check whether the file pointer is at the end of file or not int feof( FILE *stream ); Opened file pointer Returns none-zero : if it is EOF 0 : if it is not EOF
  • 17. 17 Formatted I/O Function fprintf() and fscanf() #include <stdio.h> void main() { int i, j, k ; FILE *ifp, *ofp ; ifp = fopen("abc.txt", "r" ) ; ofp = fopen("cal.txt", "w" ) ; fscanf( ifp, "%d %d %d", &i, &j, &k ) ; fprintf( ofp, "sum: %dn", i + j + k ) ; fclose( ifp ) ; fclose( ofp ) ; }  fprintf(), fscanf() Example 1 2 3 abc.txt cal.txt is not exist
  • 18. 18 Formatted I/O Function fprintf() and fscanf() #include <stdio.h> void main() { int i, j, k ; FILE *ifp, *ofp ; ifp = fopen("abc.txt", "r" ) ; ofp = fopen("cal.txt", "w" ) ; fscanf( ifp, "%d %d %d", &i, &j, &k ) ; fprintf( ofp, "sum: %dn", i + j + k ) ; fclose( ifp ) ; fclose( ofp ) ; }  fprintf(), fscanf() Example 1 2 3 abc.txt sum: 6 cal.txt
  • 19. 19 Formatted I/O Function fprintf() and fscanf() #include <stdio.h> void main() { int i, j, k ; FILE *ifp, *ofp ; ifp = fopen("abc.txt", "r" ) ; ofp = fopen("cal.txt", "a" ) ; fscanf( ifp, "%d %d %d", &i, &j, &k ) ; fprintf( ofp, "sum: %dn", i + j + k ) ; fclose( ifp ) ; fclose( ofp ) ; }  fprintf(), fscanf() Example 1 2 3 abc.txt sum: 6 cal.txt
  • 20. 20 Formatted I/O Function fprintf() and fscanf() #include <stdio.h> void main() { char c ; FILE *ifp, *ofp ; ifp = fopen("abc.txt", "r" ) ; ofp = fopen("abc2.txt", "a" ) ; while( feof(ifp) == 0 ) { fscanf( ifp, "%c", &c ) ; fprintf( ofp, "%c", c ) ; } fclose( ifp ) ; fclose( ofp ) ; }  fprintf(), fscanf() Example This is a file. 1 2 3 abc.txt This is a file. 1 2 3 abc2.txt
  • 21. 21 Misc. File I/O Functions  fgetc() , getc() – Read a character from a file • If there is no more character to read, return EOF – fgetc() is equivalent to getc() char c ; FILE *fp = fopen("abc.txt", "r" ) ; while( (c = fgetc(fp)) != EOF ) printf("%c", c ) ; int fgetc ( FILE *stream ) ;
  • 22. 22 Misc. File I/O Functions  fputc() , putc() – Write a character to a file – fputc() is identical to putc() char c ; FILE *fp = fopen("abc.txt", "r" ) ; FILE *ofp = fopen("xyz.txt", "w" ) ; while( (c = fgetc(fp)) != EOF ) fputc( c, ofp ) ; int fputc ( int c, FILE *stream ) ;
  • 23. 23 Misc. File I/O Functions  fputs() : Writing a string – puts() for file I/O – Print a string into file – At success, it returns a non-negative value – At failure, it returns EOF int fputs(char *str, FILE *fp);
  • 24. 24 Misc. File I/O Functions FILE *fp; int i; char *data[3]={"to ben","or notn","to ben"}; fp = fopen("abc.txt", "w"); for(i = 0; i<3; i++) fputs(data[i],fp); fclose( fp );
  • 25. 25 Misc. File I/O Functions  fgets() : Reading a String – gets() for file I/O – Read (num -1) characters from fp and store to str – When fp meets ‘n’ or EOF, it stops reading and store NULL into str – At success, it returns the address of str. Otherwise, it returns NULL char *fgets(char *str, int num, FILE *fp);
  • 26. 26 Misc. File I/O Functions  fgets() char s[5] ; FILE* fp = fopen("abc.txt", "r" ) ; fgets( s, 5, fp ) ; 123 1 2 1234567
  • 27. 27 Misc. File I/O Functions  Difference between gets and fgets – gets: read before ‘n’. Not store ‘n’ – fgets: read before ‘n’. Store also ‘n’
  • 28. 28 Accessing a File Randomly  How to read the file again? char c ; FILE *fp = fopen("abc.txt", "r" ) ; while( !foef(fp) ) { fscanf( fp, "%c", &c ) ; printf("%c", c ) ; } while( !foef(fp) ) { fscanf( fp, "%c", &c ) ; printf("%c", c ) ; } abcd efghi abc.txt
  • 29. 29 Accessing a File Randomly  rewind() – Move file position indicator to the beginning of the file void rewind( FILE* ) ; char c ; FILE *fp = fopen("abc.txt", “r” ) ; while( !foef(fp) ) { fscanf( fp, "%c", &c ) ; printf("%c", c ) ; } rewind( fp ) ; while( !foef(fp) ) { fscanf( fp, "%c", &c ) ; printf("%c", c ) ; } abcd efghi abc.txt
  • 30. 30 Accessing a File Randomly  fseek() – Move file position indicator to offset from place – Value of place 0 (starting point of the file) 1 (current position) 2 (end of the file) fseek( file_ptr, offset, place);
  • 31. 31 Accessing a File Randomly  fseek() fseek( file_ptr, offset, place); 0 (SEEK_SET) 1 (SEEK_CUR) 2 (SEEK_END) 0123456789 abcdefghjk abc.txt FILE *fp = fopen("abc.txt", “r” ) ; fseek(fp, 3, SEEK_SET) ; fseek(fp, 5, SEEK_CUR) ; fseek(fp, -10, SEEK_END) ;
  • 32. 32 Accessing a File Randomly  ftell() – Return the current value of file position indicator – Returned value is the number of bytes from the beginning of the file – Value of indicator is increased by one when it read a character int ftell( FILE* );
  • 33. 33 Accessing a File Randomly  ftell() char c ; int pos ; FILE *fp = fopen("abc.txt", “r” ) ; while( !foef(fp) ) { fscanf( fp, "%c", &c ) ; printf("%d: %cn", ftell(fp), c ) ; } abcd efghi abc.txt
  • 34. 34 stdin, stdout & stderr  Print output with fprintf – stdout is a file pointer for monitor  Read from keyboard with fscanf – Stdin is a file pointer for keyboard printf( “This is a testn” ) ; fprintf( stdout, “This is a testn” ) ; scanf( “%d”, &k ) ; fscanf( stdin, “%d”, &k ) ;
  • 35. 35 stdin, stdout & stderr Standard C files in stdio.h Written in C Name Remark stdin standard input file connected to the keyboard stdout standard output file connected to the screen stderr standard error file connected to the screen  Three kinds of file pointer in stdio.h
  • 36. 36 stdin, stdout & stderr #include <stdio.h> void main() { int k, j ; fscanf( stdin, "%d %d", &k, &j ) ; fprintf( stdout, "Sum: %dn", k + j ) ; fprintf( stderr, "OKn") ; }