Download presentation
Presentation is loading. Please wait.
Published byGodwin Chambers Modified over 8 years ago
2
PROGRAMMING II( Files+Structures) Tallinn 2016 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee
3
Homework (I)- example Files and structures Write an algorithm and matching code for a program with the following functional requirements: Data is read from a plain text file „F1.txt“ and stored as a structure. The data file must contain the given attributes: Name - string Age - integer Income - floating point Program will output: To file „F2.txt“ all of the entries whose age is below the average. To file „F3.txt“ all of the entries whose income is above the average.
4
File system
5
Contiguous logical address space Types: Data numeric character binary Program File Concept
6
File Structure None - sequence of words, bytes Simple record structure Lines Fixed length Variable length Complex Structures Formatted document Relocatable load file Can simulate last two with first method by inserting appropriate control characters Who decides: Operating system Program
7
File Attributes Name – only information kept in human-readable form Identifier – unique tag (number) identifies file within file system Type – needed for systems that support different types Location – pointer to file location on device Size – current file size Protection – controls who can do reading, writing, executing Time, date, and user identification – data for protection, security, and usage monitoring Information about files are kept in the directory structure, which is maintained on the disk
8
File Operations File is an abstract data type Create Write Read Reposition within file Delete Truncate Open(F i ) – search the directory structure on disk for entry F i, and move the content of entry to memory Close (F i ) – move the content of entry F i in memory to directory structure on disk
9
Open Files Several pieces of data are needed to manage open files: File pointer: pointer to last read/write location, per process that has the file open File-open count: counter of number of times a file is open – to allow removal of data from open-file table when last processes closes it Disk location of the file: cache of data access information Access rights: per-process access mode information
10
Access Methods Sequential Access read next write next reset no read after last write (rewrite) Direct Access read n write n position to n read next write next rewrite n n = relative block number
11
Operations Performed on Directory Search for a file Create a file Delete a file List a directory Rename a file Traverse the file system
12
Protection File owner/creator should be able to control: what can be done by whom Types of access Read Write Execute Append Delete List
13
Opening a file Working with files requires us to use file pointers To declare a file pointer: FILE *fp; To open a file: filePointer = fopen(fileName, mode); Both filename and mode are type char* (char pointers aka strings) File pointer will be set to NULL when opening files You should always verify that the file was opened successfully 122016
14
NULL pointer A pointer that does not refer to a valid object Behavior undefined Often used to indicate pointers not in use or failed operations End of a list (advanced data structures) Memory allocation failed (dynamic memory management) Opening file failed Search for substring failed (strstr from string.h) Assigning pointer to NULL avoids unintentional access to released resources May lead to segmentation fault when accessed NULL is a pointer, 0 is an integer. NULL pointer may not always be zero, but typically is. 201613
15
Parameters for fopen Modes: "r " – read "w" – write (when the file already exists, it will be overwritten. If there is no file, it will be created) "a" – append (write to the end of an existing file) File name: Full name of the file (name + extension if it exists) as a string (char*) The file will be looked for in the same directory as the executable (relative path) You can also use a full path to the file 142016
16
Code example You should always check if the file was successfully opened When opening of the file fails, reading from it will crash the program. FILE *fi ;// file pointer declaration char inputFile[] = {"numbers.txt"}; fi = fopen(inputFile,"r"); //opening file if (fi == NULL) // check for errors { exit(1); // abort. Exit comes from stdlib.h } 152016
17
Closing a file Resources should be freed when not needed All of the files should be closed before exiting When writing, any files not closed may have parts or all of the content missing from it. Closing writes the output still in buffer to the file. Especially important when writing log files and the program crashes. To close a file: fclose(filePointer); 162016
18
Functions with files fscanf (filePointer, format, …); read formatted text from a file fgets (destination, length, filePointer); read line by line or until length is exceeded fprintf (filePointer, format, …); write formatted text to a file fputs (contents, filePointer); write line by line to a file feof (filePointer); Check if the end-of-file indicator is set EOF - a constant to indicate the end of file. Typically -1 172016
19
Example- reading using EOF #include int main(void) { FILE *fi; int temp; char inputFile[] = {"numbers.txt"}; fi = fopen(inputFile,"r"); while (fscanf(fi, "%d", & temp) != EOF) { printf("Got: %d\n", temp); } fclose(fi); return 0; } 182016
20
Example- reading using count #include #define INPUT_FILE "numbers.txt" int main(void) { FILE *fi = fopen(INPUT_FILE,"r"); int used, quota; while (fscanf(fi, "%d %d", &used, "a) == 2) printf("User is using %d, quota was %d\n ", used, quota); fclose(fi); return 0; } 201619
21
Example - writing to the file #include #define LIMIT 5 int main(void) { FILE *fo = fopen(“numbers2.txt","w"); int i; for (i = 0; i < LIMIT; i++) fprintf(fo, "Output: %d\n", i); fclose(fo); return 0; } 202016
22
External sorting files (bonus task 4p ) F 97 20 53 04 32 15 50 89 48 95 04 37 n = 12 F1 97, 04 32 48 95 F2 20 53, 15 50 89, 04 37 F3 20 53 97, 04 37 F4 04 15 32 48 50 89 95 F1 04 15 20 32 48 50 53 89 95 97 F2 04 37 F3 04 04 15 20 32 37 48 50 53 89 95 97 F4
23
STRUCTURES
24
Complex data type It consist of other data types (int, char, float etc) typedef struct employee { int employeeCode; char firstName[15]; char lastName[15]; float salary; } employee ;
25
Structure in the memory struct employee { int employeeCode; char firstName[15]; char lastName[15]; float salary; }; 24 04193639 emplCofirstNamelastNamesalary Structure size in memory = the size of its members + padding In this case, the structure is 38 bytes + padding 2016
26
Accessing structure members Stucture member names are united by points with the structure variable name. structureVariableName.memberName manager.salary = 15.5; printf("%f", manager.salary); employees[5].salary = manager.salary; strcpy(employees[5].firstName, "Indrek"); You can assign structures as a whole employee manager, staff[20],employees[10]; employees[0] = manager; employees[5] = staff[15]; 252016
27
Example #include typedef struct point { int x; int y; } point; int main(void) {double length; point segment[2]; printf("Enter the start and the ending of a line segment x1 y1 x2 y2\n"); scanf("%d%d%d%d",&segment[0].x,&segment[0].y,&segment[1].x,&segment[1].y); length=sqrt(pow(segment[1].x - segment[0].x, 2)+pow(segment[1].y - segment[0].y,2)); printf(„The segment length is %lg\n", length); return 0; } 262016
28
Example - Initializing a structure 1 #include 2 3 typedef struct product 4 { 5 float price; 6 char name[30]; 7 } product; 8 9 int main(void) 10 { 11 product item = {.price = 0.50,.name = "Apple"}; 12 product items[] = {{0.35, "Bread"}, {0.90, "Tomato"}}; 13 printf("%.2f eur - %s\n", item.price, item.name); 14 printf("%.2f eur - %s\n", items[0].price, items[0].name); 15 printf("%.2f eur - %s\n", items[1].price, items[1].name); 16 return 0; 17 } 201627
29
Nested structures typedef struct date { unsigned day; unsigned month; unsigned year; } date; typedef struct person { char fName[15]; char lName[15]; date contractSigned; } person; 201628 You can put one structure inside of another The order they’re defined is important
30
Function returning a structure Reminder: structure was a data type, albeit a complex one. This means that we can also use it as a return type for a function. We can return one whole structure per function call. We could also return a structure pointer as we’ll see in a few weeks 201629 typedef struct point { int x, y; } point; A prototype for a function point enterPoint() point enterPoint (){ point temporary; temporary.x = 5; temporary.y = 7; return temporary; }
31
Pointer operations int *p // declare a pointer variable &var // get the location of the variable(mem address) *p // dereferencing a pointer p = &var // assigning the address of var to pointer p *p = 55 // assign a value to the dereferenced pointer printf("%p",p) // printing out the address stored in var p printf("%d", *p) // printing out the value that p points to 201630
32
Pointers and arrays visualized 201631 *p 48F9AC9 1 array[0] 5 array[1] 3 array[2] 7 array[3] 3 array[4] 5 *(p + 2)*(p + 1)*(p + 0) *(p + 3) *(p + 4) int array[] = {5, 3, 7, 3, 5}; int *p; p = array;
33
Pointers and structures You can point to the start of a structure or an element inside it. typedef struct employee{ int employeeCode; char firstName[15]; char lastName[15]; float salary; } employee; employee manager; employee *pStr; pStr = &manager; 201632
34
Accessing members using pointers (*pStr).employeeCode; (*pStr).fName; (*pStr).lName; (*pStr).salary; pStr->employeeCode; pStr->fName; pStr->lName; pStr->salary; Both of these are equal 201633
35
Example with pointers #include typedef struct employee {int employeeCode; char fName[15]; char lName[15]; float salary; } employee; void printEmployee(employee *pStr); int main(void) { employee manager = {775,"Ants", "Kukk", 555.55}; printEmployee(&manager); return 0;} void printEmployee(employee *pStr) {printf("Employee %06d, %s %s, earns %2.2f per week", pStr->employeeCode, pStr->fName, pStr->lName, pStr->salary);} 201634
36
Header files(1) For a larger project it makes sense to break the program code into several files. Custom datatypes and function prototypes are moved into.h header files. The implementation of these functions remains in the.c code files. In order to use datatypes or functions of another module you have to: 1. include the corresponding header file to your code file 2. tell the compiler to compile the code file where the functionality has been implemented as well IAG058235
37
Header files (2) tst.h typedef struct typename{ int a; float b; char s[10]; } test_struct; void printAll(int n, struct typename *M); tst.c #include #include "tst.h" void printAll(int n, struct typename *M){ int i; for(i = 0; i < n; i++) printf("%s\n", M[i].s); } IAG058236
38
Header files (3) main.c #include "tst.h" int main(void){ test_struct M[2] = {12, 3.7, "Thomas", 14, 4.4, "Matthew"}; printAll(2, M); } Compiling: gcc main.c tst.c -o test -Wall Running:./test IAG058237
39
Binary files (1) Binary files are files that contain data the way it is stored in computer memory. Some parts of binary files may be human readable but it is not their primary purpose. Data in a binary file is not editable without a specialized editor. If the binary file contains records of a single type then it is very easy to read and write data to that file using functions fread() and fwrite(). IAG058238
40
Binary files (2) Function fwrite() attempts to write nmemb number of elements from an array pointed to by ptr into a file stream. It returns the number of elements it successfully managed to write. Basically the function writes size * nmemb number of bytes from memory straight to file. The sizeof() keyword is useful for determining the size of a single element. size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) IAG058239
41
Binary files (3) Function fread() attempts to read nmemb elements from file stream into an array pointed to by ptr. It returns the number of elements it managed to read. Basically the (partial) contents of the file are copied straight to memory. size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) IAG058240
42
Binary files (4) int loadData(int max, struct data *D, char *filename) { FILE *f = fopen(filename, "rb"); if(f == NULL) { printf("Couldn't load data from file %s.\n", filename); return 0; } int k = fread(D, sizeof(struct data), max, f); fclose(f); return k; } IAG058241
43
Binary files (5) void saveData(int n, struct data *D, char *filename) { FILE *f = fopen(filename, "wb"); if(f == NULL) { printf("Couldn't save data to file %s.\n", filename); return; } fwrite(D, sizeof(struct data), n, f); fclose(f); } IAG058242
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.