Input/Output and the Operating Systems

Similar documents
System Software Experiment 1 Lecture 7

25.2 Opening and Closing a File

File IO and command line input CSE 2451

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

CSE 230 Intermediate Programming in C and C++ Input/Output and Operating System

PROGRAMMAZIONE I A.A. 2017/2018

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based

Standard I/O in C, Computer System and programming in C

2009 S2 COMP File Operations

C-Refresher: Session 10 Disk IO

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.

UNIX System Programming

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

CSI 402 Lecture 2 Working with Files (Text and Binary)

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

CS240: Programming in C

System Programming. Standard Input/Output Library (Cont d)

Input/Output: Advanced Concepts

Input / Output Functions

Standard C Library Functions

File Handling. Reference:

Introduction to file management

File I/O. Preprocessor Macros

File Access. FILE * fopen(const char *name, const char * mode);

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing

CSE2301. Introduction. Streams and Files. File Access Random Numbers Testing and Debugging. In this part, we introduce

File (1A) Young Won Lim 11/25/16

Fundamentals of Programming. Lecture 10 Hamed Rasifard

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

Data File and File Handling

Standard File Pointers

EM108 Software Development for Engineers

CSC 270 Survey of Programming Languages. Input and Output

C PROGRAMMING. Characters and Strings File Processing Exercise

UNIT IV-2. The I/O library functions can be classified into two broad categories:

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

UNIX Shell. The shell sits between you and the operating system, acting as a command interpreter

File I/O. Arash Rafiey. November 7, 2017

C PROGRAMMING Lecture 6. 1st semester

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

HIGH LEVEL FILE PROCESSING

Computer programming

Stream Model of I/O. Basic I/O in C

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

Computer Programming Unit v

Intermediate Programming, Spring 2017*

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

ENG120. Misc. Topics

Princeton University. Computer Science 217: Introduction to Programming Systems. I/O Management

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060

Princeton University Computer Science 217: Introduction to Programming Systems. I/O Management

Princeton University Computer Science 217: Introduction to Programming Systems I/O Management

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

CP2 Revision. theme: file access and unix programs

Exercise Session 3 Systems Programming and Computer Architecture

Chapter 12. Files (reference: Deitel s chap 11) chap8

M.CS201 Programming language

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

C programming basics T3-1 -

Programming & Data Structure

CSC 1107: Structured Programming

C mini reference. 5 Binary numbers 12

Lecture 9: File Processing. Quazi Rahman

CSC 1107: Structured Programming

File and Console I/O. CS449 Spring 2016

Main Program Revisited. Reading Assignment. K.N. King Chapter 3, 22. K.N. King Sections Example of Argument Processing

CSI 402 Lecture 2 (More on Files) 2 1 / 20

UNIT- 2. Binary File

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

C Programming Language

CS 261 Fall Mike Lam, Professor. Structs and I/O

Computer System and programming in C

C Basics And Concepts Input And Output

C File Processing: One-Page Summary

Memory Layout, File I/O. Bryce Boe 2013/06/27 CS24, Summer 2013 C

Ch 11. C File Processing (review)

Organization of a file

File and Console I/O. CS449 Fall 2017

structs as arguments

Student Number: Instructor: Reid Section: L0101 (10:10-11:00am)

File Processing. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

fopen() fclose() fgetc() fputc() fread() fwrite()

File I/O, Project 1: List ADT. Bryce Boe 2013/07/02 CS24, Summer 2013 C

LANGUAGE OF THE C. C: Part 6. Listing 1 1 #include <stdio.h> 2 3 int main(int argc, char *argv[]) PROGRAMMING

C Programming 1. File Access. Goutam Biswas. Lect 29

Computer Programming: Skills & Concepts (CP) Files in C

Simple Output and Input. see chapter 7

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Student Number: Instructor: Reid Section: L5101 (6:10-7:00pm)

8. Structures, File I/O, Recursion. 18 th October IIT Kanpur

Operating System Labs. Yuanbin Wu

Transcription:

Input/Output and the Operating Systems Fall 2015 Jinkyu Jeong (jinkyu@skku.edu) 1

I/O Functions Formatted I/O printf( ) and scanf( ) fprintf( ) and fscanf( ) sprintf( ) and sscanf( ) int printf(const char* format, ); int scanf(const char* format, ); int fprintf(file *fp, const char* format, ); int fscanf(file *fp, const char* format, ); int sprintf(char *s, const char* format, ); int sscanf(const char *s, const char* format, ); 2

Formating Rule (1) control_string other_arguments printf("she sells %d %s for %f", 99, "sea shells", 3.77) control_string other_arguments Conversion character Corresponding argument %d 99 %s "sea shells" %f 3.77 3

Formating Rule (2) Decimal numbers Types Hexadecimal d à x Octal d à o Conversion character Signed char %hhd %hhu short %hd %hu int %d %u long %ld %lu long long %lld %llu Unsigned 4

Formating Rule (3) Floating-point numbers Types Conversion character Decimal e-format float %f %e double %f %e long double %Lf %Le Decimal format 31.41592 e-format 3.141592e+01 Others Types Conversion character char %c string %s pointer %p 5

Formating Rule (4) Pitfalls Be careful of specifying conversion character #include <stdio.h> int main(void) { short a = 0; short b = 0; scanf("%d", &a); printf("%hd %hd\n", a, b); return 0; } $./a.out 65537 1 1 #include <stdio.h> int main(void) { long long l = 0xF00000000000FFFF; /* -1152921504606781441 */ printf("%d\n", l); return 0; } $./a.out 65536 6

Example: Format Rule in printf() Declarations and initializations char c = 'A', s[] = "Blue moon!"; Format Corresponding argument How it is printed Remarks %c c "A" Field width 1 by default %2c c " A" Field width 2, right adjusted %-3c c "A " Field width 3, left adjusted %s s "Blue moon!" Field width 10 by default %3s s "Blue moon!" More space needed %.6s s "Blue m" Precision 6 %-11.8s s "Blue moo " Field width 11, Precision 8, left adjusted 7

Example: Format Rule in printf() Declarations and initializations int i = 123; double x = 0.123456789; Format Corresponding argument How it is printed Remarks %d i "123" Field width 3 by default %05d i "00123" Padded with zeros %7o i " 173" Right adjusted, octal %-9x i "7b " Left adjusted, hexadecimal %-#9x i "0x7b " Left adjusted, hexadecimal %10.5f x " 0.12346" Field width 10, precision 5 %-12.5e x "1.23457e-01 " Field width 12, Left adjusted, precision 5, e-format 8

sscanf() and sprintf() int sprintf(char *s, const char* format, ); int sscanf(const char *s, const char* format, ); char str1[] = "1 2 3 go", str2[100], tmp[100]; int a, b, c; sscanf(str1, "%d%d%d%s", &a, &b, &c, tmp); sprintf(str2, "%s %s %d %d %d\n", tmp, tmp, a, b, c); printf("%s", str2); go go 1 2 3 9

fopen() and fclose() A file should be opened before being used Why? After used, it is better to be closed To flush the buffer (fflush) BTW, what is a file? A sequence of bytes (characters) These bytes can be accesses sequentially/randomly 10

fopen() and fclose() FILE *fopen(const char *filename, const char *mode); Performs housekeeping to use a file Access right Availability Data structures for a file Returns A file pointer on success NULL when fails Mode is either r or w or a For w or a, a new file is created if it doesn t exist r+ for open a text file for read/write rb to read a binary file, "wb" to write a binary file int fclose(file *fp); Close a file Return 0 on success 11

Standard Files The system opens the three standard files stdin, stdout, stderr printf/scanf functions work with stdout/stdin Screen/keyboard in most cases int fprintf(file *fp, const char* format, ); int fscanf(file *fp, const char* format, ); scanf( ) == fscanf(stdin, ) printf( ) == fprintf(stdout, ) 12

fgetc() and fputc() int fgetc(file *fp); int getc(file *fp); Read a character from an opened file int fputc(int c, FILE *fp); int putc(int c, FILE *fp); Write a character to an opened file 13

#include <ctype.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { int c; FILE *fp, *tmp_fp; if (argc!= 2 ) { fprintf(stderr, "\n%s%s%s\n\n%s\n\n", "Usage: ", argv[0], " filename", "This file will be doubled and some letters capitalized."); exit(1); } fp = fopen(argv[1], "r+"); tmp_fp = tmpfile(); while ((c = getc(fp))!= EOF) putc(toupper(c), tmp_fp); rewind(tmp_fp); fprintf(fp, "---\n"); while((c = getc(tmp_fp))!= EOF) putc(c, fp); return 0; } tmpfile() crease a temporary file that will be deleted when it is closed or when the program exits toupper() capitalizes a character exit() exits the program immediately rewind() rewinds a file 14

Random Access long ftell(file* fp) Returns the position fp points to in the file int fseek(file* fp, long offset, int place) Initially, fp points the beginning of the file The fp points the next byte to be accesses fseek() sets the value of fp place SEEK_SET, SEEK_END, SEEK_CUR file offset offset SEEK_SET fp fp SEEK_CUR fp fp offset SEEK_END 15

/* Write a file backwards. */ #include <stdio.h> #define MAXSTRING 100 int main(void) { char fname[maxstring]; int c; FILE *ifp; fprintf(stderr, "\ninput a filename: "); scanf("%s", fname); ifp = fopen(fname, "rb"); /* binary mode for MS_DOS */ fseek(ifp, 0, SEEK_END); /* move to end of the file */ fseek(ifp, -1, SEEK_CUR); /* back up one character */ while (ftell(ifp) > 0 ) { c = getc(ifp); /* move ahead one character */ putchar(c); fseek(ifp, -2, SEEK_CUR); /* back up two characters */ } return 0; } 16

fgets() and fputs() char *fgets(char *s, int n, FILE *fp); Read at most n-1 characters from the file associated with fp, and stores them into s If fgets() meets a newline or EOF, no more characters are read Returns s when successful NULL, otherwise int fputs(const char *s, FILE *fp); Copies the string s into the file associated with fp, except for the NULL itself c.f., puts() appends automatically appends a newline. Returns Nonnegative value when success EOF, otherwise 17

fgets() vs. gets() char *gets(char *s); Read a line from stdin Stops after an EOF or a newline Dangerous it continues to store characters past the end of s char *fgets(char *s, int size, FILE *stream); Read at most (size-1) characters Stops after an EOF or a newline Terminating null ('\0') is stored char buffer[1024]; gets(buffer); fgets(buffer, 1024, stdin); 18

fread() and fwrite() Binary stream input/output size_t fread(void *ptr, size_t size, size_t items, FILE *fp); Reads at most size*items bytes from the file associated with fp and stores into the array at ptr size_t fwrite(const void *ptr, size_t size, size_t items, FILE *fp); Reads size*items bytes from the array at ptr and writes them to the file associated with fp 19

Example: fread() and fwrite() #include <stdio.h> struct card { int pips; char suit; }; int main(void) { } struct card deck[] = { {2,'s'}, {7,'d'}, {8,'c'} }; FILE *fp = fopen( "unformatted", "wb" ); fwrite(deck, sizeof(struct card), 3, fp); fclose(fp); #include <stdio.h> struct card { int pips; char suit; }; int main(void) { } struct card deck[10]; FILE *fp = fopen( "unformated", "rb" ); fread(deck, sizeof(struct card), 10, fp); fclose(fp); printf( "pips = %d, suit = %c\n", deck[6].pips, deck[7].suit ); 20

Executing Commands int system(const chat *s); system( date ); /* date is a command */ Legal set of commands differ system to system 21

Timing There is a very accurate clock inside a computer <time.h> file defines clock_t and time_t clock_t clock(void); The time used by this program time_t time(time_t *p); Seconds elapsed since 1/1/1970 22

C Function Manuals In Linux $ man printf 23

C Function Manuals How to use man $ man man Other ways to access C manuals Google it Books MSDN (microsoft developers network) 24