cse1301 computer programming: lecture 14 i/o and files

35
CSE1301 Computer Programming: Lecture 14 I/O and Files

Upload: brianna-jenkins

Post on 05-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE1301 Computer Programming: Lecture 14 I/O and Files

CSE1301 Computer Programming:

Lecture 14I/O and Files

Page 2: 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

Page 3: CSE1301 Computer Programming: Lecture 14 I/O and Files

Low-Level Computing

0 or 1

Page 4: CSE1301 Computer Programming: Lecture 14 I/O and Files

Machine Language

Memory Address

Instructions(in hex)

OperationCodes /

Mnemonics

Page 5: CSE1301 Computer Programming: Lecture 14 I/O and Files

Compilers and Linkers

• Translate high level program into machine language.

#include <stdio.h>

int main(){ printf(“Hello World!\n”);

return 0;}

Source code Executable code

Page 6: CSE1301 Computer Programming: Lecture 14 I/O and Files

System Software

systemsoftware

applicationsoftware

applicationsoftware

applicationsoftwareuser

user

user

user

Page 7: CSE1301 Computer Programming: Lecture 14 I/O and Files

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.

Page 8: CSE1301 Computer Programming: Lecture 14 I/O and Files

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)

Page 9: CSE1301 Computer Programming: Lecture 14 I/O and Files

Operating System

driver

driver

driver

kernel

shell

Page 10: CSE1301 Computer Programming: Lecture 14 I/O and Files

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

Page 11: CSE1301 Computer Programming: Lecture 14 I/O and Files

Storage Space Allocation

• Space is allocated in blocks.

• File occupies a chain of blocks (not necessarily contiguous).

• File Allocation Table (FAT).

• Directory Information.

Page 12: CSE1301 Computer Programming: Lecture 14 I/O and Files

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.

Page 13: CSE1301 Computer Programming: Lecture 14 I/O and Files

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)

Page 14: CSE1301 Computer Programming: Lecture 14 I/O and Files

File I/O in C

• Step 0: Include stdio.h.

#include <stdio.h>

int main(){ ...

return 0;}

Page 15: CSE1301 Computer Programming: Lecture 14 I/O and Files

File I/O in C

• Step 1: Declare file handler (pointer) as FILE *.

int main(){ FILE *inputfile; FILE *outputfile; FILE *currentfile;

...

return 0;}

Page 16: CSE1301 Computer Programming: Lecture 14 I/O and Files

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

Page 17: CSE1301 Computer Programming: Lecture 14 I/O and Files

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().

Page 18: CSE1301 Computer Programming: Lecture 14 I/O and Files

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 : readw : write

a : append

Page 19: CSE1301 Computer Programming: Lecture 14 I/O and Files

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

Page 20: CSE1301 Computer Programming: Lecture 14 I/O and Files

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 handlerbecomes NULLwhen an fopen()

error occurs.

Page 21: CSE1301 Computer Programming: Lecture 14 I/O and Files

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

Page 22: CSE1301 Computer Programming: Lecture 14 I/O and Files

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

Page 23: CSE1301 Computer Programming: Lecture 14 I/O and Files

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 erroror end of input

occurs.

Page 24: CSE1301 Computer Programming: Lecture 14 I/O and Files

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

Page 25: CSE1301 Computer Programming: Lecture 14 I/O and Files

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

Page 26: CSE1301 Computer Programming: Lecture 14 I/O and Files

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

Page 27: CSE1301 Computer Programming: Lecture 14 I/O and Files

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

Page 28: CSE1301 Computer Programming: Lecture 14 I/O and Files

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 filewas not openedsuccessfully.

Page 29: CSE1301 Computer Programming: Lecture 14 I/O and Files

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

Page 30: CSE1301 Computer Programming: Lecture 14 I/O and Files

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

Page 31: CSE1301 Computer Programming: Lecture 14 I/O and Files

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.

Page 32: CSE1301 Computer Programming: Lecture 14 I/O and Files

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.

Page 33: CSE1301 Computer Programming: Lecture 14 I/O and Files

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.

Page 34: CSE1301 Computer Programming: Lecture 14 I/O and Files

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

Page 35: CSE1301 Computer Programming: Lecture 14 I/O and Files

Summary

Reading for next lecture: D&D 8.1-8.10

• Operating systems• New C functions:

– fopen(), fscanf(), fprint(), fclose()

– exit()– misc: #define