Download presentation
Presentation is loading. Please wait.
Published byBrianna Jenkins Modified over 9 years ago
1
CSE1301 Computer Programming: Lecture 14 I/O and Files
2
Topics System software Operating systems File Systems File I/O Reading: Brookshear: 3-1, 3-2, 3-3
3
Low-Level Computing 0 or 1
4
Machine Language Memory Address Instructions (in hex) Operation Codes / Mnemonics
5
Compilers and Linkers Translate high level program into machine language. #include int main() { printf(“Hello World!\n”); return 0; } Source code Executable code
6
System Software system software application software application software application software user
7
System Software Tasks: –Manage Input/Output facilities. –Load program into memory. –Initiate execution of program. –Manage memory and mass storage. Examples: –Format. –Compilers and linkers. –Communications drivers. –Operating Systems.
8
Operating Systems: Examples UNIX (Berkeley, Bell Laboratories) –Most recent version: LINUX –Flavours: System V, BSD, Posix CP-M, MS-DOS (Microsoft) OS/2, Windows95/98/2000/NT Macintosh operating system: Apple Computers. The Evolution of Operating Systems (see text)
9
Operating System driver kernel shell
10
Operating System: Functions Mediates interaction between hardware, software and user. Facilitates manipulation of programs and data. Standardizes the human/machine interface. Manages sharing of resources: –CPU, memory, peripherals, files
11
Storage Space Allocation Space is allocated in blocks. File occupies a chain of blocks (not necessarily contiguous). File Allocation Table (FAT). Directory Information.
12
The File System Managed by the Operating System. Information stored as files --- i.e. a sequence of bytes stored on a secondary storage device. Tasks: 1. Create and delete files; 2. Provide access to files; 3. Manage secondary storage space; 4. Protect files from unauthorized access; 5. Protect files against loss or damage.
13
File Organisation The file system maintains a directory of: –file names –file locations in secondary storage –file size –access control information supplied by user –administrative information (date of creation, time of last change, etc)
14
File I/O in C Step 0: Include stdio.h. #include int main() {... return 0; }
15
File I/O in C Step 1: Declare file handler (pointer) as FILE *. int main() { FILE *inputfile; FILE *outputfile; FILE *currentfile;... return 0; }
16
File I/O in C Step 2: Open file using fopen(). int main() { FILE *inputfile; FILE *outputfile; FILE *currentfile; inputfile = fopen(“Names.txt”, “r”); outputfile = fopen(“Marks.txt”, “w”); currentfile = fopen(“Logfile.txt”, “a”);... return 0; }
17
File I/O in C int main() { FILE *inputfile; FILE *outputfile; FILE *currentfile; inputfile = fopen(“Names.txt”, “r”); outputfile = fopen(“Marks.txt”, “w”); currentfile = fopen(“Logfile.txt”, “a”);... return 0; } File name Step 2: Open file using fopen().
18
File I/O in C int main() { FILE *inputfile; FILE *outputfile; FILE *currentfile; inputfile = fopen(“Names.txt”, “r”); outputfile = fopen(“Marks.txt”, “w”); currentfile = fopen(“Logfile.txt”, “a”);... return 0; } Step 2: Open file using fopen(). Mode r : read w : write a : append
19
File I/O in C Step 3: Check if file is openned successfully. int main() { FILE *inputfile; inputfile = fopen(“Names.txt”, “r”); if (inputfile == NULL) { printf(“Unable to open input file.\n”); return 1; }... return 0; }
20
File I/O in C Step 3: Check if file is openned successfully. int main() { FILE *inputfile; inputfile = fopen(“Names.txt”, “r”); if (inputfile == NULL) { printf(“Unable to open input file.\n”); return 1; }... return 0; } File handler becomes NULL when an fopen() error occurs.
21
File I/O in C Step 4a: Use fscanf() for input. #define MAXLEN 35 int main() { FILE *inputfile; char name[MAXLEN]; float mark; /*** Insert fopen and error checking here ***/ while (fscanf(inputfile, “%s”, name) != EOF) { printf(“Enter mark for %s: \n”, name);... } return 0; }
22
File I/O in C Step 4a: Use fscanf() for input. #define MAXLEN 35 int main() { FILE *inputfile; char name[MAXLEN]; float mark; /*** Insert fopen and error checking here ***/ while (fscanf(inputfile, “%s”, name) != EOF) { printf(“Enter mark for %s: \n”, name);... } return 0; } File handler
23
File I/O in C Step 4a: Use fscanf() for input. #define MAXLEN 35 int main() { FILE *inputfile; char name[MAXLEN]; float mark; /*** Insert fopen and error checking here ***/ while (fscanf(inputfile, “%s”, name) != EOF) { printf(“Enter mark for %s: \n”, name);... } return 0; } fscanf() returns EOF when error or end of input occurs.
24
File I/O in C Step 4b: Use fprintf() for output. int main() { /*** Insert variable declarations here ***/ /*** Insert fopen and error checking here ***/ while (fscanf(inputfile, “%s”, name) != EOF) { printf(“Enter mark for %s: \n”, name); scanf(“%f”, &mark); fprintf(outputfile, “%s %f\n”, name, mark); } return 0; }
25
File I/O in C Step 4b: Use fprintf() for output. int main() { /*** Insert variable declarations here ***/ /*** Insert fopen and error checking here ***/ while (fscanf(inputfile, “%s”, name) != EOF) { printf(“Enter mark for %s: \n”, name); scanf(“%f”, &mark); fprintf(outputfile, “%s %f\n”, name, mark); } return 0; } File handler
26
File I/O in C Step 4b: Use fprintf() for output. int main() { /*** Insert variable declarations here ***/ /*** Insert fopen and error checking here ***/ while (fscanf(inputfile, “%s”, name) != EOF) { printf(“Enter mark for %s: \n”, name); scanf(“%f”, &mark); fprintf(outputfile, “%s %f\n”, name, mark); } return 0; } fprintf() returns negative when an error occurs.
27
File I/O in C Step 5: Close file using fclose(). int main() { FILE *inputfile; FILE *outputfile; FILE *currentfile;... fclose(inputfile); fclose(outfile); fclose(currentfile); return 0; }
28
File I/O in C Step 5: Close file using fclose(). int main() { FILE *inputfile; FILE *outputfile; FILE *currentfile;... fclose(inputfile); fclose(outfile); fclose(currentfile); return 0; } Clears input/ output buffer. fclose() fails when the file was not opened successfully.
29
#include main() { FILE * file; char nextChar; if ((file = fopen("HelpFile.txt", "r") == NULL) { printf(“Error opening file.\n”); exit(1); } while(fscanf(file,"%c", &nextChar)!=EOF) { printf("%c",nextChar); } fclose(file); } Example: ShowFile (Alg. 21)
30
FILE * inFile; FILE * outFile; char nextch; inFile = fopen("helpfile.txt", "r"); outFile = fopen("outputfile.txt", "w"); if ((inFile == NULL) || (outFile == NULL)) { printf(“Error opening file.\n”); exit(1); } while (fscanf(inFile, "%c", &nextch) != EOF) { fprintf(outFile, "%c", nextch); } fclose(inFile); fclose(outFile); Exercise
31
Backup-up and Recovery The system maintains copies of all files. Copies can be made from one disk to another, or from disk to tape. How often are copies made? –periodically; OR –incrementally: file is copied only when it is created or altered.
32
Advantages and Disadvantages Periodical Back-up: –large volumes of information must be copied each time; –loss of files that were created or altered since last copy i.e. hours of work by user! Incremental Back-up –reduces the amount of copying performed; –copies are more up to date; –more difficult to administer.
33
File Access and Security General types of access: read, write, execute File Access on MS-DOS –assume one user, so only single level –can make files read-only to prevent being overwritten or deleted –to execute a file, must be particular type:.EXE,.BAT,.COM,.BIN You are using a variant of DOS called Novell.
34
File Access on UNIX User's own access: read, write, execute (3 bits) "Group" access: read, write, execute (3 bits) "World" (other) access: read, write, execute (3 bits) Example:110 100 000 rw- r- - - - -
35
Summary Reading for next lecture: D&D 8.1-8.10 Operating systems New C functions: –fopen(), fscanf(), fprint(), fclose() –exit() –misc: #define
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.