CSE1301 Computer Programming: Lecture 14 I/O and Files
Topics System software Operating systems File Systems File I/O Reading: Brookshear: 3-1, 3-2, 3-3
Low-Level Computing 0 or 1
Machine Language Memory Address Instructions (in hex) Operation Codes / Mnemonics
Compilers and Linkers Translate high level program into machine language. #include int main() { printf(“Hello World!\n”); return 0; } Source code Executable code
System Software system software application software application software application software user
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.
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)
Operating System driver kernel shell
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
Storage Space Allocation Space is allocated in blocks. File occupies a chain of blocks (not necessarily contiguous). File Allocation Table (FAT). Directory Information.
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.
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)
File I/O in C Step 0: Include stdio.h. #include int main() {... return 0; }
File I/O in C Step 1: Declare file handler (pointer) as FILE *. int main() { FILE *inputfile; FILE *outputfile; FILE *currentfile;... return 0; }
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; }
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().
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
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 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.
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 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
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.
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 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
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.
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; }
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.
#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)
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
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.
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.
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.
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: rw- r
Summary Reading for next lecture: D&D Operating systems New C functions: –fopen(), fscanf(), fprint(), fclose() –exit() –misc: #define