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

Slides:



Advertisements
Similar presentations
COMPUTERS: TOOLS FOR AN INFORMATION AGE Chapter 3 Operating Systems.
Advertisements

File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
OPERATING SYSTEM An operating system is a group of computer programs that coordinates all the activities among computer hardware devices. It is the first.
Operating System.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Chapter 4 : File Systems What is a file system?
COSC 120 Computer Programming
1 ICS103 Programming in C Lecture 8: Data Files. 2 Outline Why data files? Declaring FILE pointer variables Opening data files for input/output Scanning.
11/13/01CS-550 Presentation - Overview of Microsoft disk operating system. 1 An Overview of Microsoft Disk Operating System.
File Management Systems
1 CSE1301 Computer Programming: Lecture 21 File I/O.
Cs238 Lecture 3 Operating System Structures Dr. Alan R. Davis.
Computer Forensics Principles and Practices by Volonino, Anzaldua, and Godwin Chapter 6: Operating Systems and Data Transmission Basics for Digital Investigations.
Chapter 12 File Management Systems
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
CSE1301 Computer Programming: Lecture 19 File I/O
An Introduction to Operating Systems. Definition  An Operating System, or OS, is low-level software that enables a user and higher-level application.
Lecture 11 – Files Operation. Introduction Almost all of the program developed before this is interactive In interactive environment, input is via keyboard.
System Calls 1.
1 Chapter 12 File Management Systems. 2 Systems Architecture Chapter 12.
Operating System. Architecture of Computer System Hardware Operating System (OS) Programming Language (e.g. PASCAL) Application Programs (e.g. WORD, EXCEL)
OPERATING SYSTEM OVERVIEW. Contents Basic hardware elements.
OS provide a user-friendly environment and manage resources of the computer system. Operating systems manage: –Processes –Memory –Storage –I/O subsystem.
1 CSE1301 Computer Programming: Lecture 19 File I/O.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
OSes: 11. FS Impl. 1 Operating Systems v Objectives –discuss file storage and access on secondary storage (a hard disk) Certificate Program in Software.
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
C o n f i d e n t i a l 1 Course: BCA Semester: III Subject Code : BC 0042 Subject Name: Operating Systems Unit number : 1 Unit Title: Overview of Operating.
OS, , Part I Operating - System Structures Department of Computer Engineering, PSUWannarat Suntiamorntut.
1 Data Structures and Algorithms Programs. Different problems. Operations. Size of data. Resources available.
We will focus on operating system concepts What does it do? How is it implemented? Apply to Windows, Linux, Unix, Solaris, Mac OS X. Will discuss differences.
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
Chapter 1 Computers, Compilers, & Unix. Overview u Computer hardware u Unix u Computer Languages u Compilers.
ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.
1 CSC103: Introduction to Computer and Programming Lecture No 27.
Files A collection of related data treated as a unit. Two types Text
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
ECE 456 Computer Architecture Lecture #9 – Input/Output Instructor: Dr. Honggang Wang Fall 2013.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
File-System Management
DKT121: Fundamental of Computer Programming
ECE Application Programming
Operating System.
Protection of System Resources
Structured Programming II
Lecture 8: Variable Scope & Working with Files
EKT120: Computer Programming
File I/O.
What is an Operating System?
File Input/Output.
Computer Programming Lecture 15 Text File I/O
Computer Electronic device Accepts data - input
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Computer Electronic device Accepts data - input
Modern PC operating systems
LINUX System : Lecture 7 Lecture notes acknowledgement : The design of UNIX Operating System.
Files Operations.
Department of Computer Science
EECE.2160 ECE Application Programming
The File Manager Implementation issues
Files Chapter 8.
Presentation transcript:

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