Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Formatted Files.

Slides:



Advertisements
Similar presentations
C: Advanced Topics-II Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Advertisements

Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CS1061: C Programming Lecture 19: Random Access Files A. O’Riordan, 2004, 2007 updated.
Lec11: File Processing 廖雪花 TEL: 年 5 月.
Structures and Unions Chapter 6. Structure A structure is an aggregate data type  Composed of two or more related variables called member/field/element.
 Types of files  Command line arguments  File input and output functions  Binary files  Random access.
C: Advanced Topics Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 File Handling in C Lecture 17c 20/3/01.
File Processing 檔案處理. 檔案 I/O 作業系統的關係 使用者程式 C 標準 I/O 函示庫 UNIXMS_DOS System call.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Winter 2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Assignment-II Tips.
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.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
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 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
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 Introduction Introduce some standard library functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
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.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
C Programming Lecture 12 : File Processing
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 29 Thanks for Lecture Slides:
‘ typedef ’ is a keyword,which allows you to specify a new name for a datatype which is already defined in c language program. Syntax: typedef /* Re-defining.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
Adv. UNIX:fp/101 Advanced UNIX v Objectives of these slides: –a more detailed look at file processing in C Special Topics in Comp. Eng.
C: Advanced Topics Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
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.
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.
11 C File Processing.
Lecture 12 CIS 208 Friday, March 3rd , 2005.
Chapter 4 File Processing
Lecture 11 File input/output
TMF1414 Introduction to Programming
Introduction to Computer Programming Lecture 18 Binary Files
CS111 Computer Programming
What you need for the 1st phase of project
Introduction to Programming
Chapter 11 – File Processing
Lecture 15 Files.
Computing Science Thompson Rivers University
FILE HANDLING IN C.
Text and Binary File Processing
Fundamental of Programming (C)
File Handling in C.
Chapter 12: File I/O.
FILE handeling.
EPSII 59:006 Spring 2004.
Professor Jodi Neely-Ritz University of Florida
I/O CS580U - Fall 2018.
Files Chapter 8.
Presentation transcript:

Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Formatted Files

File Handling TRU-COMP2130 C: Advanced Topics 2 There are different types of files: a. Unformatted Files a. Text Files (which we have already covered) b. Formatted Files a. Text Files with fprintf and fscanf b. Binary Files

Text Files TRU-COMP3710 Introduction 3 We have discussed the text files (even with command line arguments) The steps are: Open a file Write or read Close the file

Formatted files TRU-COMP3710 Introduction 4 The files created with fprintf, can be read using fscanf in the same sequence The steps are again the same Open/create the file Read/write on the file (using fscanf/fprintf) Close the file

Formatted Files TRU-COMP We can use the file versions of scanf and printf, called fscanf and fprintf. General format: fscanf (file_pointer, control_string, list) ; fprintf (file_pointer, control_string, list) ; Examples: fscanf (fp, “%d %s %f”, &roll, dept_code, &cgpa) ; fprintf (out, “\nThe result is: %d”, xyz) ;  Be very careful that the formats should remain the same in fscanf and fprintf

Binary Files TRU-COMP3710 Introduction 6 Binary files are very similar to arrays of structures, except the structures are in a disk-file rather than an array in memory. There are two features, that distinguish binary files from text files: You can instantly use any structure in the file. You can change the contents of a structure anywhere in the file. The steps are: Create/open the file Write/read the structure Close the file

Operations on Binary File TRU-COMP3710 Introduction 7 Open the file with “b” in the mode After opening, one can read and write a structure on the file It is also possible to seek a specific position in the file. The position is at record 0 when the file is opened. A read operation reads the structure where the file position indicator is pointing to. After reading the structure the pointer is moved to point at the next structure. A write operation will write to the currently pointed-to structure. After the write operation the file position indicator is moved to point at the next structure. The fseek function will move the file position indicator to the record that is requested. The ftell returns the current value of the file pointer

Common File Access operations TRU-COMP2130 C: Advanced Topics 8 #include FILE *in, *out; // FILE is defined in in = fopen(“in_filename”, “r”);// mode: r, w, a, r+, w+, a+ if (in == NULL)... out = fopen(“out_filename”, “w”); fclose(in); fprintf(out, “format...”, variables...); fscanf(...); fgets(...);

Opening and using binary files TRU-COMP3710 Introduction 9 The opening modes may be:

Special binary file operations TRU-COMP3710 Introduction 10 int fseek(FILE*, long, SEEK_SET or SEEK_CURRENT or SEEK_END); // move file position pointer int fwrite(void*, int memb_size, int no_memb, FILE*); int fread(void*, int memb_size, int no_memb, FILE*); int ftell ( FILE * stream );

Defining the structure TRU-COMP3710 Introduction 11 The record or the structure is defined as: struct member { int mem_id; char mem_name[25]; int salary; }; * We may use typedef to define the user defined datatype as well

To create the file and add records TRU-COMP3710 Introduction 12 void create() { fp=fopen("Binary.txt", "ab"); struct member m; int ans=1; while (ans==1) { printf("\nEnter Member ID : "); scanf("%d", &m.mem_id); printf("\nEnter Member Name : "); scanf("%s",m.mem_name); printf("\nEnter Salary : "); scanf("%d", &m.salary); fwrite(&m, sizeof(struct member),1,fp); // this is writing the address of the structure on the file printf("\n\nPress '1' to add more and '0' to go back : "); scanf("%d", &ans); if (ans==0) {fclose(fp); return;} if (ans!=0 && ans !=1) printf("Invalid option entered "); } fclose(fp); }

Reading all the records TRU-COMP3710 Introduction 13 void display() { fp=fopen("Binary.txt", "rb"); struct member mem; int a; while(1) { fread(&mem, sizeof(struct member), 1, fp); // read a specific record at the record pointer if (feof(fp)) break; printf("\nMemebr ID : %d\nMember Name : %s\nSalary : %d\n", mem.mem_id, mem.mem_name, mem.salary); } printf("Press '0' to go back "); scanf("%d", &a); fclose(fp); }

Reading a specific record TRU-COMP3710 Introduction 14 In order to read a specific record, the number of record is taken from the user and then fseek is used to move the record pointer void search() { int a; struct member mem; fp=fopen("Binary.txt", "rb"); while(1) { printf("Enter the record number to search : "); scanf("%d", &a); fseek(fp, sizeof(struct member)*(a-1), 0); // seeking the location from the beginning of the file fread(&mem, sizeof(struct member),1, fp); printf("\nMemebr ID : %d\nMember Name : %s\nSalary : %d\n", mem.mem_id, mem.mem_name, mem.salary); printf("Press '1' to search more and '0' to go back : "); scanf("%d", &a); if (a==0) {fclose(fp); return;} if (a!=0 && a!=1) printf("Invalid input "); }

Fseek() TRU-COMP3710 Introduction 15 The fseek may be used to travel anywhere in the file: int fseek ( FILE * stream, long int offset, int origin ); The origin may be: Offset would be negative with SEEK_END

Rewind() TRU-COMP3710 Introduction 16 This positions the record pointer to the beginning of the file void rewind ( FILE * stream );

Update a record TRU-COMP3710 Introduction 17 First the record is read and it may also be written back with the new values void update() { int a; fp=fopen("Binary.txt", "r+"); //this would open in read as well as write mode char name_temp[25]; int sal_temp; struct member mem; while(1) { printf("Enter the record number to search : "); scanf("%d", &a); fseek(fp, sizeof(struct member)*(a-1), 0); //this is searching the specific record fread(&mem, sizeof(struct member),1, fp); printf("\n\nEnter new name : "); scanf("%s", mem.mem_name); //inputting the new name and salary printf("\n\nEnter new salary : "); scanf("%d", &mem.salary); fseek(fp, -sizeof(struct member), SEEK_CUR); //we need to go back to the original position to write back fwrite(&mem,sizeof(struct member), 1, fp); printf("\n\n Record Updated "); }

Display the record TRU-COMP3710 Introduction 18 After reading the record, the simple display is: printf("\nMemebr ID : %d\nMember Name : %s\nSalary : %d\n", mem.mem_id, mem.mem_name, mem.salary); If we wish to define a function to display the members values: showOne(&mem); It is passing the address and pointer would receive the value

Using pointer values TRU-COMP3710 Introduction 19 void showOne(struct member *ptr) { printf("\nMember ID : %d", ptr->mem_id); //we use -> in spite of. printf("\nMember Name : %s", ptr->mem_name); printf("\nSalary : %d\n\n", ptr->salary); }

TRU-COMP2130 C: Advanced Topics 20 void print_rcd(struct student_rcd rcd) { printf(“Number: %d\n”, rcd.student_number); printf(“Name: %s\n”, rcd.name); // name is an array. // not &(rcd.name) } void read_rcd(struct student_rcd *rcd) { printf(“Enter number: “); scanf(“%d”, &(rcd->student_number)); // reference rqd printf(“Enter name: “); scanf(“%s”, rcd->name); // name is an array. // not &(rcd->name) }

Delete a record TRU-COMP3710 Introduction 21 Once created, we may edit the data, but cannot delete Read all records and transfer to other file except the one’s you wish to delete Rename the new file while (1) { fread(&mem,sizeof(struct member),1,fp); //reading from fp if (feof(fp)) break; if (strcmp(naam, mem.mem_name) == 0) { printf("A record with requested name found and deleted.\n\n"); found=1; } else { fwrite(&mem, sizeof(struct member), 1, fp_tmp); //writing on fp_tmp } remove("Binary.txt"); rename("tmp.bin", "Binary.txt");

Some extra methods: TRU-COMP3710 Introduction 22 void menu() //to show all the menu options { system("clear"); printf("\n\n\t\tM A I N M E N U \n"); printf("\t1. Create the Binary File \n"); printf("\t2. Display the contents of the file \n"); printf("\t3. Seek a specific record \n"); printf("\t4. Update the contents of a record \n"); printf("\t5. Delete a record for the specific name \n"); printf("\t6. Exit \n"); } int input() //to take user input {int a; printf("\n\t\tPlease Enter your choice.... "); scanf("%d", &a); return(a); }

Screen shots TRU-COMP3710 Introduction 23

Formatted files TRU-COMP3710 Introduction 24 To create a file void create() { fp=fopen("format.txt", "a"); int mem_id; char mem_name[25]; int salary; int ans=1; while (ans==1) { printf("\nEnter Member ID : "); scanf("%d", &mem_id); printf("\nEnter Member Name : "); scanf("%s",mem_name); printf("\nEnter Salary : "); scanf("%d", &salary); fprintf(fp, "%d %s %d\n",mem_id, mem_name, salary); printf("\n\nPress '1' to add more and '0' to go back : "); scanf("%d", &ans); if (ans==0) {fclose(fp); return;} if (ans!=0 && ans !=1) printf("Invalid option entered "); } fclose(fp); }

Read TRU-COMP3710 Introduction 25 To read the file void display() { fp=fopen("format.txt", "r"); int mem_id; char mem_name[25]; int salary; int a; while(!feof(fp)) { fscanf(fp, "%d %s %d\n", &mem_id, mem_name, &salary); printf("\n\t Member ID : %d \n\tMember Name : %s \n\tSalary : %d\n", mem_id, mem_name, salary); } printf("Press '0' to go back "); scanf("%d", &a); fclose(fp); }

Search a member_id TRU-COMP3710 Introduction 26 while(1) { found=0; printf("Enter the record number to search : "); scanf("%d", &a); rewind(fp); while(!feof(fp)) { fscanf(fp, "%d %s %d\n", &mem_id, mem_name, &salary); if (mem_id==a) { found=1; printf("\nMemebr ID : %d\nMember Name : %s\nSalary : %d\n", mem_id,mem_name, salary); } if (!found) printf("This Member_ID is not present "); printf("Press '1' to search more and '0' to go back : "); scanf("%d", &a); if (a==0) {fclose(fp); return;} if (a!=0 && a!=1) printf("Invalid input "); }