Lesson 2-4 File Processing2 Fundamental File Processing Operations.
Lesson 2-4 File Processing2 Fundamental File Processing Operations.
Fundamental File
Processing Operations
Kim Joung-Joon
Database Lab.
jjkim9@db.konkuk.ac.kr
Chapter Objectives
Chapter Outline
2.1 Physical Files and Logical Files
2.2 Opening Files
2.2 Closing Files
2.4 Reading and Writing
2.5 Seeking
2.6 Special Characters in Files
2.7 The Unix Directory Structures
2.8 Physical Devices and Logical Files
2.9 File-Related Header Files
2.10 Unix File System Commands
File Structures (2)
File
Physical file
Logical file
Closing a file
the logical name or file descriptor is available for use with
another file (i.e., breaks the link)
ensure that everything has been written to the file
(i.e., the buffer for the file has been flushed of data and
everything we have written has been sent to the file)
automatically closed by OS when a program terminates
normally
(=> for protection against data loss and for reuse of logical
filenames)
byte count
Size)
read, write
1.
2.
5.
10
Detecting End-of-file
C++
11
Seeking
12
0 :
1 :
2 :
13
14
devices such as tape or disk drivers are also files (in dev
directory)
current directory : .
parent directory : ..
15
bin
usr
bin
lib
usr6
lib
dev
mydir
adb cc yacc
console
kbd
TAPE
libdf.a
libc.a libm.a
addr
DF
16
17
defined in stdio.h
18
Pipe
19
C streams : stdio.h
20
Unix Commands
cat filenames
tail filenames
cp file1 file2
rm filenames
ls
rmdir name
mv file1 file2
chmod mode filename
mkdir name
21
22
assign(input_file, myfile.dat);
// associate between a logical file and a physical file
reset(input_file); // open existing file
rewrite(input_file);
// create new file
append(input_file);
// open to add data to existing file
read(input_file, var);
// read from file to variable
readln(input_file, var);
// read from file to variable
write(input_file, var);
// write from variable to file
writeln(input_file, var);
// write from variable to file
close(input_file); // close file
23
Low-level I/O
UNIX system calls
24
A.4 <stdio.h>
fp = fopen(s, mode)
/* open file s; mode r, w, a for read, write, append (returns NULL for error) */
c = getc(fp)
/* get character; getchar() is getc(stdin) */
putc(c, fp)
/* put character; putchar(c) is putc(c, stdout) */
ungetc(c, fp)
/* put character back on input file fp; at most 1 char can be pushed back at one time */
scanf(fmt, a1, ....)
/* read characters from stdin into a1, ... according to fmt. Each ai must be a pointer. Returns EOF or
number of fields converted */
fscanf(fp, .....)
/* read from file fp */
printf(fmt, a1, ....)
/* format a1, ... according to fmt, print on stdout */
fprintf(fp, ....)
/* print .... on file fp */
fgets(s, n, fp)
/* read at most n characters into s from fp. Returns NULL at end of file */
fputs(s, fp)
/* print string s on file fp */
fflush(fp)
/* flush any buffered output on file fp */
fclose(fp)
/* close file fp */
25
#include <fstream.h>
26
Class Hierarchy
File Structures (2)
27
28
29
30
31