Bài giảng Introduction to computer programming - Chapter 9: File Processing

Ho Chi Minh City University of Technology  
Faculty of Computer Science and Engineering  
Chapter 9: File Processing  
Introduction to Computer Programming  
(C language)  
TS. Võ Thị Ngọc Châu  
2017 – 2018, Semester 2  
Course Content  
C.1. Introduction to Computers and  
Programming  
C.2. C Program Structure and its  
Components  
C.3. Variables and Basic Data Types  
C.4. Selection Statements  
C.5. Repetition Statements  
C.6. Functions  
C.7. Arrays  
C.8. Pointers  
C.9. File Processing  
2
References  
[1] “C: How to Program”, 7th Ed. – Paul  
Deitel and Harvey Deitel, Prentice Hall, 2012.  
[2] “The C Programming Language”, 2nd Ed.  
– Brian W. Kernighan and Dennis M. Ritchie,  
Prentice Hall, 1988  
and others, especially those on the Internet  
3
Content  
Introduction  
Declare files  
Open and close files  
Store and retrieve data from files  
Use macros  
Summary  
4
Recall – Chapter 3 and Chapter 8  
Memory layout of a C program  
Higher address  
Command-line arguments  
and environment variables  
Local variables, arguments,  
grown/shrunk with function calls  
Stack  
Grown/shrunk with dynamic  
allocation and de-allocation  
Heap  
Uninitialized (static) global  
variables, static local variables  
Uninitialized data  
Initialized to  
zero by exec  
.bss  
Initialized (static) global variables,  
static local variables, constants  
Initialized data  
Read from  
program file  
by exec  
.data  
Code  
.text  
Machine code, often read-only  
Lower address  
bss = block started by symbol, better save space  
Introduction  
Data (input, output, supporting) in memory  
Temporary as lost when a program terminates !!!  
Can we have permanent data before and  
after program execution?  
Files on secondary storage devices (hard  
drives, CDs, DVDs, flash drives, …)  
6
Introduction  
Files on hard drives  
data1.txt  
data2.txt  
…
7
Introduction  
Figure 11.1. Data hierarchy  
[1], pp. 419  
8
Introduction  
C views each file as a sequential stream of bytes.  
Each file ends either with an end-of-file marker or at  
a specific byte number recorded in a system-  
maintained, administrative data structure.  
end-of-file: ctrl-z (Windows), ctrl-d (Linux/Mac OS X/Unix)  
When a file is opened, a stream is associated with the  
file.  
Streams provide communication channels between  
files and programs.  
Figure 11.2. C’s view of a file of n bytes  
[1], pp. 420  
9
Introduction  
C views each file as a sequential stream of  
bytes.  
Fixed-length records stored in a random-access file  
The exact location of a record relative to the  
beginning of the file  
Figure 11.10: C’s view of a random-access file  
[1], pp. 431  
10  
Introduction  
Operations on files  
Create  
Open  
Read  
Write (write a new file, append an existing file)  
Close  
Access to files in C for processing  
Sequential access to a sequential stream of bytes  
Random access to a stream of fixed-size records  
11  
Introduction  
The standard library: <stdio.h>  
FILE *fopen(const char *filename, const char *mode)  
FILE *freopen(const char *filename, const char *mode,  
FILE *stream)  
int fclose(FILE *stream)  
size_t fread(void *ptr, size_t size, size_t nmemb, FILE  
*stream)  
int fgetc(FILE *stream)  
char *fgets(char *str, int n, FILE *stream)  
int fscanf(FILE *stream, const char *format, ...)  
size_t fwrite(const void *ptr, size_t size, size_t nmemb,  
FILE *stream)  
int fputc(int char, FILE *stream)  
int fputs(const char *str, FILE *stream)  
int fprintf(FILE *stream, const char *format, ...)  
12  
Declare files  
Each file communicated with C program via a  
stream controlled by a pointer of FILE  
identifier: a valid identifier for a pointer  
pointing to a file stream  
FILE* identifier;  
FILE: an object type suitable for storing  
information for a file stream, defined in  
the <stdio.h> standard library  
FILE* pFile1;  
FILE* pFile2 = 0;  
FILE* pFile3 = NULL;  
FILE*: a pointer type of FILE type  
A file pointer will be associated with a file  
stream once a file is opened.  
13  
Open and close files  
Open files  
FILE *fopen(const char *filename, const char  
*mode)  
Opens the file pointed to by filename using the given  
opening mode.  
pFile1 = fopen("Data.txt", "r");  
Close files  
int fclose(FILE *stream)  
Closes the stream. All buffers are flushed.  
fclose(pFile1);  
14  
Open and close files  
Open files  
Filename = "Data.txt"  
Located in the current directory  
Filename = ".\\Data\\Data.txt"  
Located in the sub-directory of the current directory  
Filename = "..\\Data.txt"  
Located in the super-directory of the current directory  
Filename = "D:\\CS - Introduction to Computer  
Programming - CO1003 - Undergraduate  
course\\Code samples\\Data\\Data.txt"  
Located in the specified directory with the absolute path  
15  
…  
Open and close files  
File opening modes  
16  
Store and retrieve data from files  
Store data into files (write)  
A sequence of bytes from memory to the file  
The file plays a role of stdout.  
Retrieve data from files (read)  
A sequence of bytes from the files to memory  
The file plays a role of stdin.  
What bytes?  
A char  
Read (file -> memory)  
Write (memory -> file)  
fgetc  
fgets  
fputc  
fputs  
fprintf  
fwrite  
A line of bytes  
Formatted bytes fscanf  
Bytes in the  
binary mode  
fread  
Store and retrieve data from files  
Store and retrieve data from files  
Store and retrieve data from files  
What bytes?  
Read (file -> memory)  
Write (memory -> file)  
A char  
fgetc  
fputc  
char aChar;  
A line of bytes  
char aStr[50];  
aChar = fgetc(pFile1);  
fgets  
fputc('a', pFile1);  
fputs  
fgets(aStr, 50, pFile1);  
fputs("Today?", pFile1);  
fprintf  
Formatted bytes fscanf  
int anInt;  
char aChar;  
float aFloat;  
fscanf(pFile1, "%d %c  
%f", &anInt, &aChar,  
&aFloat);  
fprintf(pFile1, "%d %d  
%c %f", 10, anInt, aChar,  
aFloat);  
Bytes in the  
binary mode  
fread  
fwrite  
char buffer[50]; fread(buffer,  
fwrite(buffer,  
sizeof(int)+sizeof(char)+si strlen(buffer)+1, 1,  
zeof(float)+1, 5, pFile1); pFile1);  
Tải về để xem bản đầy đủ
pdf 38 trang yennguyen 19140 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Introduction to computer programming - Chapter 9: File Processing", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_introduction_to_computer_programming_chapter_9_fil.pdf