14. fiile io
-
Upload
- -
Category
Technology
-
view
623 -
download
0
description
Transcript of 14. fiile io
![Page 1: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/1.jpg)
File I/O
![Page 2: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/2.jpg)
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
![Page 3: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/3.jpg)
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
![Page 4: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/4.jpg)
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
![Page 5: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/5.jpg)
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”);
![Page 6: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/6.jpg)
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”);
![Page 7: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/7.jpg)
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”);
![Page 8: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/8.jpg)
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+”);
![Page 9: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/9.jpg)
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+”);
![Page 10: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/10.jpg)
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+”);
![Page 11: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/11.jpg)
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 */
![Page 12: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/12.jpg)
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)
![Page 13: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/13.jpg)
13
File Operation Function fopen()
fclose() Example
#include <stdio.h>
void main() {
FILE *fp ;
fp = fopen(“my_file.txt”, “r”);
...
fclose( fp ) ;
}
![Page 14: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/14.jpg)
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 %d\n”, 1, 2 );
![Page 15: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/15.jpg)
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 );
![Page 16: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/16.jpg)
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
![Page 17: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/17.jpg)
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: %d\n", i + j + k ) ;
fclose( ifp ) ;
fclose( ofp ) ;
}
fprintf(), fscanf() Example
1 2 3
abc.txt
cal.txt is not exist
![Page 18: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/18.jpg)
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: %d\n", i + j + k ) ;
fclose( ifp ) ;
fclose( ofp ) ;
}
fprintf(), fscanf() Example
1 2 3
abc.txt
sum: 6
cal.txt
![Page 19: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/19.jpg)
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: %d\n", i + j + k ) ;
fclose( ifp ) ;
fclose( ofp ) ;
}
fprintf(), fscanf() Example
1 2 3
abc.txt
sum: 6
cal.txt
![Page 20: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/20.jpg)
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
![Page 21: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/21.jpg)
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 ) ;
![Page 22: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/22.jpg)
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 ) ;
![Page 23: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/23.jpg)
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);
![Page 24: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/24.jpg)
24
Misc. File I/O Functions
FILE *fp;
int i;
char *data[3]={"to be\n","or not\n","to be\n"};
fp = fopen("abc.txt", "w");
for(i = 0; i<3; i++) fputs(data[i],fp);
fclose( fp );
![Page 25: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/25.jpg)
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);
![Page 26: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/26.jpg)
26
Misc. File I/O Functions
fgets()
char s[5] ;
FILE* fp = fopen("abc.txt", "r" ) ;
fgets( s, 5, fp ) ;
123
1 2
1234567
![Page 27: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/27.jpg)
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’
![Page 28: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/28.jpg)
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
![Page 29: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/29.jpg)
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
![Page 30: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/30.jpg)
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);
![Page 31: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/31.jpg)
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) ;
![Page 32: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/32.jpg)
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* );
![Page 33: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/33.jpg)
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: %c\n", ftell(fp), c ) ;
}
abcd efghi
abc.txt
![Page 34: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/34.jpg)
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 test\n” ) ; fprintf( stdout, “This is a test\n” ) ;
scanf( “%d”, &k ) ; fscanf( stdin, “%d”, &k ) ;
![Page 35: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/35.jpg)
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
![Page 36: 14. fiile io](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461de8daf79599e2c8b484e/html5/thumbnails/36.jpg)
36
stdin, stdout & stderr
#include <stdio.h>
void main() {
int k, j ;
fscanf( stdin, "%d %d", &k, &j ) ;
fprintf( stdout, "Sum: %d\n", k + j ) ;
fprintf( stderr, "OK\n") ;
}