System Software Experiment 1 Lecture 7

Similar documents
Input/Output and the Operating Systems

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

File IO and command line input CSE 2451

Standard File Pointers

File Handling. Reference:

Input / Output Functions

CS240: Programming in C

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

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

Standard C Library Functions

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

Introduction to file management

EM108 Software Development for Engineers

PROGRAMMAZIONE I A.A. 2017/2018

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

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

Intermediate Programming, Spring 2017*

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

ENG120. Misc. Topics

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

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

C-Refresher: Session 10 Disk IO

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

Data File and File Handling

25.2 Opening and Closing a File

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

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

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

Computer Programming Unit v

C mini reference. 5 Binary numbers 12

C File Processing: One-Page Summary

2009 S2 COMP File Operations

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

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

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

C Basics And Concepts Input And Output

Naked C Lecture 6. File Operations and System Calls

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

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

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

UNIX System Programming

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Lecture6 File Processing

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

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

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

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

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Lecture 9: File Processing. Quazi Rahman

File I/O. Preprocessor Macros

Ch 11. C File Processing (review)

C Programming Language

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

Computer programming

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

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

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

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

Lecture 8. Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

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

Computer System and programming in C

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)

C PROGRAMMING. Characters and Strings File Processing Exercise

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

M.CS201 Programming language

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

Fundamentals of Programming. Lecture 10 Hamed Rasifard

C programming basics T3-1 -

DS: CS Computer Sc & Engg: IIT Kharagpur 1. File Access. Goutam Biswas. ect 29

Chapter 11 File Processing

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

Week 9 Lecture 3. Binary Files. Week 9

Data Files. Computer Basics

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

Engineering program development 7. Edited by Péter Vass

File Handling in C. EECS 2031 Fall October 27, 2014

Chapter 12. Text and Binary File Processing. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

Goals of this Lecture

CSE 12 Spring 2016 Week One, Lecture Two

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

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

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

Advanced C Programming Topics

LAB 13 FILE PROCESSING

Programming & Data Structure

File and Console I/O. CS449 Spring 2016

Introduction to Computer and Program Design. Lesson 6. File I/O. James C.C. Cheng Department of Computer Science National Chiao Tung University

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

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

LAB 13 FILE PROCESSING

CSE 12 Spring 2018 Week One, Lecture Two

CS1003: Intro to CS, Summer 2008

Course organization. Course introduction ( Week 1)

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

SAE1A Programming in C. Unit : I - V

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

Introduction to Computer Programming Lecture 18 Binary Files

Transcription:

System Software Experiment 1 Lecture 7 spring 2018 Jinkyu Jeong ( jinkyu@skku.edu) Computer Systems Laboratory Sungyunkwan University http://csl.skku.edu SSE3032: System Software Experiment 1, Spring 2018 1

printf Character Printed as Example %c Character ( 문자 ) A %d, %i Decimal integer -123 %u Unsigned decimal integer 4294967173 %o Unsigned octal integer 37777777605 %x, %X Unsigned hexadecimal integer ffffff85, FFFFFF85 %e, %E Floating point number with e or E 5.284100e-01, 5.284100E-01 %f Floating point number (precision default : 6) 0.528410 %g, %G Shorter format between %f and %e 0.52841 %s String ( 문자열 ) (hexadecimal) %p Pointer address %% Print %

printf char str[] = Blue moon! Character Printed as Example %12s Field width 12, right-adjusted Blue moon! %.7s Precision 5 Blue mo %-11.8s Precision 8, Field width 11, left-adjusted Blue moo %05d Padded with zeroes 00123 %-#9x 0x is attached to hexadecimal number 0x7b %10.5f Precision 5, Field width 10 0.12346 %-12.5e Precision 5, Field width 12, e-format 1.23457e-01_

scanf int scanf(const char * format, void * input); Return the number of successfully read characters. Return EOF for read error. Basically, scanf ignores whitespace characters. Whitespace character : space( ), \n, \t To let scanf receive whitespace characters, change format. Ex) scanf( %[^\n], str); Read until the function meets \n

scanf This is the end of my input toward stdin. stdin 22 \n \n scanf( %d, &age); int age = 22; Fetch data before WS!

scanf stdin \n scanf( %c, &grade); int age = 22; char grade = \n ; Fetch only one character!

stdin, stdout, stderr stdout Standard stream input/output Defined in stdio.h stdin : connected to the keyboard PEN \n PINEAPPLE \n stdout, stderr : connected to the screen

fopen, fclose FILE * fopen (const char *filename, const char * mode); Returns File pointer Mode Character r w a Function int fclose(file * stream); Return 0 if successed. Open for read-only r+ Open for update Create for writing, if exists, overwrite. Open for appending (Write from the end-of-file) w+ Open for update (overwrite if already exists) a+ Open for appending

File pointer Input.txt PEN PINEAPPLE APPLE PEN FILE *fp = fopen(./input.txt, r ); fp Input.txt P E N P I N E A P P L E \n A P P L offset 0 4 8 12 16 Pointing location can be changed via various functions. (fscanf, fgetc, fgets, fseek, fread.) Stdin, stdout, stderr : special file pointers. - ftell(fp) : return current offset of fp. - rewind(fp) : rewind fp to the beginning

File seek: fseek fseek(fp, 6, SEEK_CUR) Input.txt PEN PINEAPPLE APPLE PEN SEEK_SET : From the beginning of the file SEEK_END : From the end of the file fp fp Input.txt P E N P I N E A P P L E \n A P P L offset 0 4 8 12 16 - ftell(fp) : return current offset of fp. - rewind(fp) : rewind fp back to the beginning.

fprintf, fscanf In stdio.h int fprintf(file *ofp, const char *format, ); int fscanf(file *ifp, const char *format, ); Return The number of printed charcters The number of data the function read If failed -1 EOF fprintf(stdout, ); = printf( ); fscanf(stdin, ); = scanf( );

fgetc, fputc, fgets, fputs No need do specify the format - For one character, int fputc (int chr, FILE *stream); int fgetc (FILE *stream); - For the string int fputs(const char *str, FILE *stream); char *fgets(char *str, int size, FILE *stream); Return character (ASCII) character (ASCII) Return Positive number Make a program recording the sentence you entered through the screen (Using fputs, fgets) str If failed EOF EOF If failed EOF null (str not changed) * at the end of file

fgets vs fscanf PEN PINEAPPLE APPLE PEN fgets(str[0], 100, fp); fgets(str[1], 100, fp); P E N P I N E A P P L E \n \0 A P P L E P E N \n \0 Includes whitespace characters. Stops at \n fscanf(fp, %s, str); fscanf(fp, %s, str); P E N \0 P I N E A P P L E \0 Omit whitespace characters Stop at whitespace characters

fgets vs fscanf PEN PINEAPPLE APPLE PEN fscanf(fp, %s, str); fgets(str[1], 15, fp); P E N \0 P I N E A P P L E \n \0 fgets(str[1], 15, fp); fscanf(fp, %s, str); P E N P I N E A P P L E \n \0 A P P L E \0

fgets vs fscanf (\n) PEN PINEAPPLE APPLE PEN fscanf(fp, %s, str[0]); P E N \0 fscanf(fp, %s, str[1]); P I N E A P P L E \0 fgets(str[2], 100, fp); fscanf(fp, %s, str[1]); \n \0 A P P L E \0

EOF, feof How to recognize we have reached the end of file? EOF : End Of File fgets returns 0 when they meet the end. while(fgets(str, sizeof(str), fp)!= 0){.. } // do something until the program reads the end of file.

EOF, feof feof(file * stream); Returns 1 if fp exceeds end of file (!= EOF), 0 if not. while(!feof(fp)){ } fgets(str, sizeof(str), fp); printf( %s, str); What happens? Make a program adding all the numbers in the file.

fread, fwrite size_t fread (void * ptr, size_t size, size_t count, FILE * stream); size_t fwrite (const void * ptr, size_t size, size_t count, FILE * stream); Returns successfully conducted number of elements Error if count!=return, or EOF Not interrupted by whitespace characters.

File access Make a program double spacing a file.

Exercise : Censorship Censor bad words.

fgets vs fscanf (\n) PEN PINEAPPLE APPLE PEN fgets(str[0], 15, fp); \0 fgets(str[1], 15, fp); P E N P I N E A P P L E \0 fscanf(fp, %[^\n], str[2]); fscanf(fp, %s, str[1]); \0 A P P L E \0

fgets vs fscanf (\n) PEN PINEAPPLE APPLE PEN fgets(str[0], 15, fp); \n \0 fgets(str[1], 15, fp); P E N P I N E A P P L E \n \0 fscanf(fp, %[^\n], str[2]); fscanf(fp, %s, str[1]); A P P L E P E N \0 \0 fscanf(fp, %s, str[0]); P E N \0 fgets(str[1], 15, fp); P I N E A P P L E \n \0 fscanf(fp, %s, str[2]); fgets(str[3], 15, fp); A P P L E \0 P E N \n \0