File Processing 檔案處理. 檔案 I/O 作業系統的關係 使用者程式 C 標準 I/O 函示庫 UNIXMS_DOS System call.

Slides:



Advertisements
Similar presentations
Using Files Declare a variable, called file pointer, of type FILE * Use function fopen to open a named file and associate this file with the file pointer.
Advertisements

Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Formatted Files.
Chapter 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
CSCI 171 Presentation 12 Files. Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines.
1 生物計算期末作業 暨南大學資訊工程系 2003/05/13. 2 compare f1 f2  只比較兩個檔案 f1 與 f2 ,比完後將結果輸出。 compare directory  以兩兩比對的方式,比對一個目錄下所有檔案的相 似程度。  將相似度很高的檔案做成報表輸出,報表中至少要.
第一章 變數、常數及資料型態. 變數 C 程式語言的變數名稱 第一個字必須是英文字母或底線 (_) 之後可以是數字, 英文字母或底線 (_) 不可以是保留字 例: Num (Ο) _score (Ο) C&C (X) 8num (X)
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Structures and Unions Chapter 6. Structure A structure is an aggregate data type  Composed of two or more related variables called member/field/element.
Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式.
File I/O 範例講解 授課:林哲嘉 日期: 2009/04/29. 範例:上機考第三題 Add 部分 1. 將檔案的資料顯示在畫面 2. 將要加入檔案加到資料的尾端.
Chapter 7Input and Output Speaker: Lung-Sheng Chien.
第十三章 檔案. 檔案 I/O 的基本觀念 檔案 I/O 的流程 定義指向 FILE 結構的指標 開啟一檔案 呼叫檔案 I/O 庫存函數以進行檔案的處理 關閉檔案 FILE 結構 此結構宣告於 標頭檔中.
 Types of files  Command line arguments  File input and output functions  Binary files  Random access.
N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
File Handling In C By - AJAY SHARMA. We will learn about- FFile/Stream TText vs Binary Files FFILE Structure DDisk I/O function OOperations.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Programming Languages -1 (Introduction to C) files Instructor: M.Fatih AMASYALI
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Text and Binary File Processing 程式設計 潘仁義 CCU COMM.
CSEB114: Principle of programming Chapter 11: Data Files & File Processing.
ECE 103 Engineering Programming Chapter 44 File I/O Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
STRUCTURE 為一種複合式的資料型態,可用來把相關的資料組織 成為有意義的資料單元。 名片: name age tel namecard : char name[10]; int age; char tel[10] char [60]
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
C Program Design C File Processing 主講人:虞台文. Content Introduction Data Hierarchy Files and Streams Creating a Sequential-Access File Reading Data from.
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
Using Files Declare a variable, called file pointer, of type FILE * Use function fopen to open a named file and associate this file with the file pointer.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Text and Binary File Processing 程式設計 潘仁義 CCU COMM.
C Programming Lecture 12 : File Processing
Structured Programming Approach Module VIII - Additional C Data Types File Handling Prof: Muhammed Salman Shamsi.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 29 Thanks for Lecture Slides:
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
Files Programming 1. 2 What is a File? Is a block of arbitrary information, or resource for storing information, which is available to a computer program.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
1 Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating a Sequential Access File 11.5Reading.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
11 C File Processing.
Chapter 4 File Processing
Lecture 11 File input/output
TMF1414 Introduction to Programming
File Handling in C.
CS111 Computer Programming
What you need for the 1st phase of project
File Handling in C.
Text and Binary File Processing
Chapter 11 – File Processing
CSC215 Lecture Input and Output.
CSC215 Lecture Input and Output.
Input/Output and the Operating Systems
FILE HANDLING IN C.
Text and Binary File Processing
File Handling in C.
Fundamental of Programming (C)
FILE handeling.
EPSII 59:006 Spring 2004.
Chapter 11 Files chap8.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Professor Jodi Neely-Ritz University of Florida
I/O CS580U - Fall 2018.
Presentation transcript:

File Processing 檔案處理

檔案 I/O 作業系統的關係 使用者程式 C 標準 I/O 函示庫 UNIXMS_DOS System call

檔案概念 Thisafie l stream is a opened file C 把檔案看成是字元串列,亦稱為 stream

File 型別 Thisafie l file name length R/W file structure DOS 與 I/O 函示 真正的檔案 disk

檔案的開啟和關閉 fopen() : #include “stdio.h” char* filename, *mode; FILE *stream; stream = fopen(filename, mode); Mode : r, w, a

檔案的開啟和關閉 fclose() : #include “stdio.h” FILE *stream; stream = fclose(stream);

DOS 啟動時會固定開啟五個檔案 stdio.h #define _NFILE 20 最多可開啟 20 個檔案 實際只有 15 個檔案 stdin, IN, text stdout, OUT, text stderr, OUT, text stdaux, I/O, binary Stdprn, OUT, binary

C 的檔案 I/O 函示群 fgetc() 、 fputc() fgetc(): read a char from a opened stream int r; FILE *stream; r = fgetc(stream); fputc(): write a char to a opened stream int r; FILE *stream; r = fputc(c, stream);

C 的檔案 I/O 函示群 fgets() 、 fputs() fgetts(): read a string from a opened stream char *s; int n; FILE *stream; p = fgets(s, n, stream); // if p=s, it is successful. fputs(): write a string to a opened stream char *s; int n; FILE *stream; p = fputs(s, n, stream); // 0x0d ans 0x0a

C 的檔案 I/O 函示群 fscanf() 、 fprintf() fscanf(): scanf a string from a opened stream char *cs; int n; FILE *stream; ptr1, ptr2,… n = fscanf(stream, cs, ptr1, ptr2,..,); fprintf(): print a string to a opened stream char *cs; int n; FILE *stream; arg1, arg2,….. n = fprintf(stream, cs, arg1, arg2);

循序與隨機讀寫 檔案位址與指位器 Thisafie l pos

循序與隨機讀寫 fseek(): move file position pointer long offset; int mode; FILE *stream; int ret; ret = fseek(stream, offset, mode); ret == 0, ret!=0 fail SEEK_SET : from the file head SEEK_CUR : from the current position SEEK_END : from the file end

Binary Read and Write size_t fread(void* ptr, szie_t size, size_t nitem, FILE* stream); size_t fwrite(void* ptr, szie_t size, size_t nitem, FILE* stream); void *ptr; // the address of read or write buffer size_t size; // the size of a data item size_t nitem; // the number of data items FILE* stream;

Case Study : Phone Book struct record{ int id; char name[20]; char phone[20]; }; Data structure : typedef struct record RD;

Case Study : Phone Book void add_record(RD*); void delete_record(RD*); void search_record(RD*); void display_all(RD*); void quit();

Case Study : Phone Book RD a[RECORD_NUM]={{0,"",""},}; if( (fp=fopen("myphone.txt", "r")) != NULL){ fread(a, sizeof(RD), RECORD_NUM, fp); fclose(fp); } else { fp=fopen("myphone.txt", "w"); fwrite(a, sizeof(RD), RECORD_NUM, fp); fclose(fp); } Create a phone book

Case Study : Phone Book while(1) { clrscr(); printf("WELECOME MY TELEPHONE BOOK !!\n"); printf("1. SAVE A RECORD\n"); printf("2. DELETE A RECORD\n"); printf("3. SEARCH A RECORD\n"); printf("4. DISPLAY ALL RECORD\n"); printf("5. QUIT\n"); printf("YOUR CHOICE(1-5)\n"); scanf("%d", &choice); switch(choice) { case 1: add_record(a); break; case 2: delete_record(a); break; case 3: search_record(a); break; case 4: display_all(a); break; case 5: quit(a); } Menu

Case Study : Phone Book while(stop == NO){ clrscr(); printf("ADD or MODIFY A RECORD\n"); printf("Enter ID number (1-100):\n"); scanf("%d", &newrecord.id); printf("Enter USER NAME:\n"); scanf("%s", newrecord.name); printf("Enter USER PHONE NUMBER:\n"); scanf("%s", newrecord.phone); if( a[newrecord.id-1].id != 0) { printf("RECORD%d has exited, do you want to update this record ? \ (NO:0 ; YES:1)\n", newrecord.id); scanf("%d", &ans); if(ans == 1) a[newrecord.id-1] = newrecord; } else a[newrecord.id-1] = newrecord; printf("to continue record addition (press 0) or quit (press 1)\n"); scanf("%d", &stop); } Add_record

Case Study : Phone Book while(stop==NO) { clrscr(); printf("DELETE A RECORD\n"); printf("Enter ID number\n"); scanf("%d", &id); a[id-1].id=0; printf("Do you want to continue delete records (press 0) or quit(press 1)\n"); scanf("%d", &stop); } Delete record

Case Study : Phone Book while(stop == NO) { id=0; clrscr(); printf("SEARCH A RECORD\n"); printf("Enter USER name:\n"); scanf("%s", name); while( strcmp(name, a[id].name) != 0 && id<RECORD_NUM) id++; if(id<RECORD_NUM) printf("%5d%20s%20s\n", a[id].id, a[id].name, a[id].phone); else printf("NOT FOUND !!\n"); printf("Do you want to continue (press 0) or quit (press 1) ?\n"); scanf("%d", &stop); } Search record

Case Study : Phone Book while(stop==NO) { clrscr(); count=0; for(i=0 ; i<RECORD_NUM ; i++) { if( a[i].id != 0){ printf("%5d%25s%25s\n", a[i].id, a[i].name, a[i].phone); count++; } if(count == 25){ getch(); count=0; } printf("Do you want to redisply all records ?(0:YES ; 1:NO)\n"); scanf("%d", &stop); } Display record

Case Study : Phone Book void quit(RD* a) { FILE *fp; fp=fopen("myphone.txt","w"); fwrite(a, sizeof(RD), RECORD_NUM, fp); fclose(fp); exit(0); } Quit system