computers and programming the 7 th lecture jiří Šebesta

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

Upload: ashlyn-heath

Post on 17-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

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

Computers and programming

The 7th lecture

Jiří Šebesta

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

TOPIC – functions for files and strings

1. Files with library stdio.h2. Files and database - example

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

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.

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

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.

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

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

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

Files with library stdio.h (4/6)

#include <stdio.h>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

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

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 <stdio.h>

int main(void){

int n, num; // index for loop and numberchar 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

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

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 addressif (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 fileprintf("%d. name: %s\n", num, name);

}fclose(ptrfile); // close the filegetchar();return 0;

}

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

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)

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

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.

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

Files and database – example (3/10)

typedef struct t_course{

char abbrev[5];char type;int credits;int year;char semester;int points;

};

Structure for course

Insert it to the header file “index.h“

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

Files and database – example (4/10)

Listing of courses

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

Files and database – example (5/10)

Adding of a course and study mean computation

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

Files and database – example (6/10)

Main program with control ops. main.c (1/2)#include <stdio.h>#include <stdlib.h>#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);

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

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

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

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

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

Files and database – example (9/10)

Hierarchy of function usage

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

Files and database – example (10/10)

Hierarchy of functions

Example: Ex67.c + index.h + index.c

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

TOPIC OF THE NEXT LECTURE

Coupling among dynamic variables

THANK YOU FOR YOUR ATTENTION