Upload
-
View
627
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
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 %d\n”, 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: %d\n", 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: %d\n", 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: %d\n", 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 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 );
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: %c\n", 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 test\n” ) ; fprintf( stdout, “This is a test\n” ) ;
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: %d\n", k + j ) ;
fprintf( stderr, "OK\n") ;
}