Computers and programming The 7 th lecture Jiří Šebesta.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to C Programming
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
C Language.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
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.
Lecture 2 Introduction to C Programming
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Introduction to C language
C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Lecture 11 – Files Operation. Introduction Almost all of the program developed before this is interactive In interactive environment, input is via keyboard.
Computers and programming The 6 th lecture Jiří Šebesta.
Computers and programming The 3 rd lecture Jiří Šebesta.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Computers and programming The 10 th lecture Jiří Šebesta.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Reading the data from the input devices and displaying the results on the screen are the two main tasks of any program. To perform these tasks user friendly.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Computers and programming 1 The 11 th lecture Jiří Šebesta.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
Computers and Programming CPC The 2 nd lecture Jiří Šebesta.
A Level Computing#BristolMet Session Objectives#U2 S7 MUST understand the difference between an array and record SHOULD be able to estimate the size of.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
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:
Pascal Programming Today Chapter 11 1 Chapter 11.
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.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
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.
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)
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
LINKED LISTS.
Chapter 7 Text Input/Output Objectives
ECE Application Programming
A bit of C programming Lecture 3 Uli Raich.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
TMF1414 Introduction to Programming
Chapter 7 Text Input/Output Objectives
Command Line Arguments
File Access (7.5) CSE 2031 Fall July 2018.
CS111 Computer Programming
MATLAB: Structures and File I/O
Arrays, For loop While loop Do while loop
Lecture 13 Input/Output Files.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Topics Introduction to File Input and Output
files Dr. Bhargavi Goswami Department of Computer Science
Today’s Lecture I/O Streams Tools for File I/O
Text and Binary File Processing
File Handling.
Fundamental of Programming (C)
Files.
Topics Introduction to File Input and Output
EPSII 59:006 Spring 2004.
Topics Introduction to File Input and Output
Files Chapter 8.
Presentation transcript:

Computers and programming The 7 th lecture Jiří Šebesta

TOPIC – functions for files and strings 1.Files with library stdio.h 2.Files and database - example

Files with library stdio.h (1/6) FILE A special type defined in library stdio.h called as FILE (need to use capital letters) – type, which can pointed a file function FILE *fopen(filename, attribute) The file filename is open for operations defined by the attribute attribute. It returns pointer of type FILE* to the open file.

Files with library stdio.h (2/6) Attributes of function fopen() ”r” - Open a file for reading (The file must exist.) ”w” Create an empty file for writing. (If a file with the same name already exists its content is erased and the file is treated as a new empty file.) ”a” Append to a file. Writing operations append data at the end of the file. (The file is created if it does not exist.) ”r+” Open a file for update both reading and writing. (The file must exist.) ”w+” Create an empty file for both reading and writing. (If a file with the same name already exists its content is erased and the file is treated as a new empty file.) ”a+” Open a file for reading and appending at the end of the file, protecting the previous content to be overwritten.

Files with library stdio.h (3/6) function int fprintf(pointer to the file, formatted string) Writes to the pointed file a sequence of data formatted as the format argument specifies. function int fclose(pointer to file) Closes the defined file. Example: Build-up a program as console application, which reads six names and saves them to the text file (each name needs to be written in new line, order is counted from 1) in form: order. name

Files with library stdio.h (4/6) #include int main(void) { int n;// index for loop char name[20];// string FILE *ptrfile;// spec. type pointer to file ptrfile = fopen("testfile.txt","w"); // open a new file for writing and obtaining address if (ptrfile!=NULL) // if file is open for(n=0 ; n<6 ; n++) { printf("Insert %d. name: \n", n); gets(name); fprintf (ptrfile, "%d %s\n", n, name); //put } formatted string into the file fclose(ptrfile);// close the file return 0; } Example: Ex65.c

Files with library stdio.h (5/6) function int fscanf(pointer to the file, formatted string) Reads data from the file and stores them according to the para- meter format into the locations pointed by the additional arguments. #include int main(void) { int n, num;// index for loop and number char name[20];// string Example: Build-up program as console application, which reads six orders and names from text file (see Ex68.c) and print them to the console window

Files with library stdio.h (6/6) Example: Ex66.c FILE *ptrfile;// spec. type pointer to file ptrfile = fopen("testfile.txt","r"); // open an existing file for reading and obtaining its address if (ptrfile!=NULL) // if file is open for(n=0; n<6 ; n++) { fscanf(ptrfile, "%d.", &num); //read number from the file fscanf(ptrfile, "%s", name); //read string from the file printf("%d. name: %s\n", num, name); } fclose(ptrfile);// close the file getchar(); return 0; }

Files and database – example (1/10) Task: Create a program as console application for course evaluation register The record will contain following items: – course abbreviation (string, e.g. BPC1E), – type = obligation (character, e.g. P = obligatory) – number of credits (integer) – year (integer) – semester (character, Z = winter, L = summer) – points (integer)

Files and database – example (2/10) Design a structure for above mentioned data and create an array for 100 pointers to this structure. Fill structures (records) by data obtained from the text file, in which data are stored sequentially in separate lines, last course, which is not used in evaluation register, is identified by abbreviation XXXX. Add a function for listing of courses in the index including grade printing (from A to F) generated based on achieved points for given course. Create a function for adding of a new record for new course Build-up functions for calculation of credits and study mean with printing to the console.

Files and database – example (3/10) typedef struct t_course { charabbrev[5]; chartype; intcredits; intyear; charsemester; intpoints; }; Structure for course Insert it to the header file “index.h“

Files and database – example (4/10) Listing of courses

Files and database – example (5/10) Adding of a course and study mean computation

Files and database – example (6/10) Main program with control ops. main.c (1/2) #include #include "index.h" int main(void) { T_course *my_index[100]; // ptrs. to courses int no_courses; // number of records char cmd; // command letter no_courses = fill_index(my_index); printf("ELECTRONIC INDEX \n\nInsert command: \n\n'q' = quit \n'p' = print full index, \n'a' = add a new course, \n's' = print study mean\n"); scanf("%c", &cmd); fflush(stdin);

Files and database – example (7/10) Main program with control ops. main.c (2/2) while (cmd != 'q')// if not quit { switch (cmd) { case 'p': print_index(my_index, no_courses); break; case 'a': no_courses = insert_course( my_index, no_courses); break; case 's': print_stmean(my_index, no_courses); } printf("\n\nInsert command: … \n"); scanf("%c", &cmd); fflush(stdin); }; save_erase_index(my_index, no_courses); return 0; }

Files and database – example (8/10) Executive functions in own library index.h int add_course(T_course **rec, int num, char *new_abbrev, char new_type, int new_credits, int new_year, char new_semester, int new_points); char get_grade(int points); float get_eval(int points); int credit_sum(T_course **rec, int num); float study_mean(T_course **rec, int num); int fill_index(T_course **rec); int insert_course(T_course **rec, int num); void print_index(T_course **rec, int num); void print_stmean(T_course **rec, int num); void save_course(char *save_abbrev, char save_type, int save_credits, int save_year, char save_semester, int save_points, FILE *pf); void save_erase_index(T_course **rec, int num);

Files and database – example (9/10) Hierarchy of function usage

Files and database – example (10/10) Hierarchy of functions Example: Ex67.c + index.h + index.c

TOPIC OF THE NEXT LECTURE Coupling among dynamic variables THANK YOU FOR YOUR ATTENTION