Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Computers and programming The 7 th lecture Jiří Šebesta."— Presentation transcript:

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

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

3 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.

4 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.

5 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

6 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

7 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

8 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; }

9 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)

10 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.

11 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“

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

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

14 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);

15 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; }

16 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);

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

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

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


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

Similar presentations


Ads by Google