Day14 A. Young W. Lim Tue. Young W. Lim Day14 A Tue 1 / 15

Similar documents
Day14 A. Young W. Lim Thr. Young W. Lim Day14 A Thr 1 / 14

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

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

Day02 A. Young W. Lim Sat. Young W. Lim Day02 A Sat 1 / 12

Input / Output Functions

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

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

File IO and command line input CSE 2451

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

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

Standard File Pointers

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

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

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

Standard C Library Functions

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

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

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

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)

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

Introduction to file management

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

Day21 A. Young W. Lim Wed. Young W. Lim Day21 A Wed 1 / 13

Basic and Practice in Programming Lab 10

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

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

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

Text Output and Input; Redirection

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

Organization of a file

C Basics And Concepts Input And Output

C programming basics T3-1 -

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

Day08 A. Young W. Lim Mon. Young W. Lim Day08 A Mon 1 / 27

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

CE Lecture 11

Simple Output and Input. see chapter 7

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

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

Computer Programming Unit v

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

SWEN-250 Personal SE. Introduction to C

Procedural Programming

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

8. Characters, Strings and Files

System Software Experiment 1 Lecture 7

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

ENG120. Misc. Topics

Goals of this Lecture

I/O Management! Goals of this Lecture!

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

I/O Management! Goals of this Lecture!

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

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

File I/O. Preprocessor Macros

The Design of C: A Rational Reconstruction (cont.)

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

Contents. A Review of C language. Visual C Visual C++ 6.0

CSE 12 Spring 2016 Week One, Lecture Two

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

CS240: Programming in C

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

Fundamentals of Programming. Lecture 11: C Characters and Strings

IO = Input & Output 2

The Design of C: A Rational Reconstruction (cont.)" Jennifer Rexford!

GAS Tutorial - 6. Expression

Fundamentals of Programming. Lecture 10 Hamed Rasifard

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

25.2 Opening and Closing a File

Advanced C Programming Topics

Fundamentals of Programming. Lecture 15: C File Processing

Fundamentals of Programming & Procedural Programming

Input/Output and the Operating Systems

M.CS201 Programming language

2009 S2 COMP File Operations

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

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

HIGH LEVEL FILE PROCESSING

C File Processing: One-Page Summary

Programming & Data Structure

LESSON 4. The DATA TYPE char

The Design of C: A Rational Reconstruction: Part 2

Continued from previous lecture

Input/Output Systems Prof. James L. Frankel Harvard University

CSE 12 Spring 2018 Week One, Lecture Two

PROGRAMMAZIONE I A.A. 2017/2018

Lecture 9: File Processing. Quazi Rahman

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

Help Session 2. Programming Assignment 2

Input/Output: Advanced Concepts

UNIX input and output

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

Chapter 11 File Processing

C-Refresher: Session 10 Disk IO

Programming Fundamentals

DATA STRUCTURES USING C

Link 7. Dynamic Linking

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

Transcription:

Day14 A Young W. Lim 2017-12-26 Tue Young W. Lim Day14 A 2017-12-26 Tue 1 / 15

Outline 1 Based on 2 C Strings (1) Characters and Strings Unformatted IO Young W. Lim Day14 A 2017-12-26 Tue 2 / 15

Based on "C How to Program", Paul Deitel and Harvey Deitel I, the copyright holder of this work, hereby publish it under the following licenses: GNU head Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License. CC BY SA This file is licensed under the Creative Commons Attribution ShareAlike 3.0 Unported License. In short: you are free to share and make derivative works of the file under the conditions that you appropriately attribute it, and that you distribute it only under a license compatible with this one. Young W. Lim Day14 A 2017-12-26 Tue 3 / 15

Characters a character constant is an int value a character is represented by single quotes the value of a character constant is the character s integer value in the machines character set ASCII Young W. Lim Day14 A 2017-12-26 Tue 4 / 15

Strings a series of characters treated as a single unit may include letters, digits various special characters such as +, -, *, / and $. $string literal$s or $string constant$s are in double quotes a string is accessed via a pointer to its first character the value of a string is the the address of its first character Young W. Lim Day14 A 2017-12-26 Tue 5 / 15

Intialization a character array can be initialized with a string constant. char a[ 20 ] = "Hello, world!"; each element of the array a can be changed a charatter pointer can be intialized with a string constant. char *p = "Hello, world!"; no element of the string constant can be modified Young W. Lim Day14 A 2017-12-26 Tue 6 / 15

Character Pointer Initialization (1) char *s = "Hello, World!"; a character pointer s is declared with an initialization the value of s is an address of a memory location where a character resides "Hello, World!" is a constant character string stored in the read-only memory region (defined by a compiler) "Hello, World!" returns the address of the 1st character in the string (the address of H ) s points to this address of the 1st character Young W. Lim Day14 A 2017-12-26 Tue 7 / 15

Character Pointer Initialization (2) char *s = "Hello, World!"; s[ 5 ]=0 causes a run-time error (Segmentation Faults) though this string is a string constant, it is not explicitly declared with const, therefore, no error message will be shown during compilation but during execution, the "Segmentation fault" error occurs because s[ 5 ]=0 attempts to change its element in the read-only memory location. we can compile but cannot execute normally. Young W. Lim Day14 A 2017-12-26 Tue 8 / 15

Types of Unformatted IO stdio file character getc putc fgetc fputc getchar putchar string gets puts fgets fputs c : character s : string f : file get : read, input put : write, output getchar() = getc(stdin) putchar(c) = putc(c, stdout) gets(s) = fgets(s, stdin)~ do not use gets insecure (no bound check) Young W. Lim Day14 A 2017-12-26 Tue 9 / 15

Standard I/O Library Functions fgets reads characters until a newline character or the end-of-file character is encountered arguemnt : an array of type char the maximum number of characters that can be read the stream from which to read a null character is appened to the array after finishing char *fgets(char *s, int size, FILE *stream); Young W. Lim Day14 A 2017-12-26 Tue 10 / 15

getc/putc vs fgetc/fputc stdio file character getc putc fgetc fputc getc can be implemented as a macro fgetc cannot be implemented as a macro the argument to getc should not be an expression with side effects since fgetc is guaranteed to be a function, pointer to fgetc can be used calls to fgetc probably take longer than calls to getc Advanced Programming in Unix Environment practically, no significant differences getc(stream) = fgetc(stream) putc(c, stream) = fputc(c, stream) Young W. Lim Day14 A 2017-12-26 Tue 11 / 15

Stream a common, logical interface to the various devices a stream is be a logical interface to a file a disk file a tape file a port the screen (stdout) the keyboard (stdin) Although files differ in form and capabilities, all streams are the same. (a uniform interface) https://www.le.ac.uk/users/rjm1/cotter/page_74.htm Young W. Lim Day14 A 2017-12-26 Tue 12 / 15

fopen and fclose FILE * fopen ( const char * filename, const char * mode ); int fclose ( FILE * stream ); "r" "w" "a" "r+" "w+" "a+" read write append read/update write/update append/update #include <stdio.h> int main () { FILE * pfile; // pfile stream pfile = fopen ("test.txt","w"); if (pfile!=null) { fputs ("fopen example",pfile); fclose (pfile); } } Young W. Lim Day14 A 2017-12-26 Tue 13 / 15

getchar() and a buffer there is an underlying buffer/stream when you enter text, the text is stored in a buffer somewhere the enter key must be pressed before getchar() gets anything to read getchar() can stream through the buffer one character at a time each read returns a character until it reaches the end of the buffer (EOF) until you press CTRL+D (end of file) https: //stackoverflow.com/questions/3676796/how-does-getchar-work Young W. Lim Day14 A 2017-12-26 Tue 14 / 15

EOF EOF isn t a character that exists in the stream, but a sentinel value to indicate when the end of the input has been reached. https: //stackoverflow.com/questions/3676796/how-does-getchar-work Young W. Lim Day14 A 2017-12-26 Tue 15 / 15