[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 7. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Similar documents
CS2141 Software Development using C/C++ Stream I/O

Streams and Basic File I/O Tools for Stream I/O Character I/O Inheritance

File I/O Christian Schumacher, Info1 D-MAVT 2013

Objects and streams and files CS427: Elements of Software Engineering

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

Chapter 12. Streams and File I/O

Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

Advanced I/O Concepts

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Simple File I/O.

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

Chapter 12. Streams and File I/O. Copyright 2016 Pearson, Inc. All rights reserved.

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Input and Output File (Files and Stream )

Chapter Overview. I/O Streams as an Introduction to Objects and Classes. I/O Streams. Streams and Basic File I/O. Objects

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

Chapter 3 - Notes Input/Output

I/O Streams and Standard I/O Devices (cont d.)

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams

What we will learn about this week:

Week 3: File I/O and Formatting 3.7 Formatting Output

BITG 1113: Files and Stream LECTURE 10

Scientific Computing

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

Writing a Good Program. 7. Stream I/O

CS103L SPRING 2018 UNIT 7: FILE I/O

Streams - Object input and output in C++

Programming in C/C

File Input/Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #10

Chapter 3: Input/Output

File I/O CS 16: Solving Problems with Computers I Lecture #9

C++ Input/Output: Streams

Random File Access. 1. Random File Access

Chapter 14 Sequential Access Files

A SHORT COURSE ON C++

Chapter 21 - C++ Stream Input/Output

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() {

Summary of basic C++-commands

Programming in C/C

C++ Programming Lecture 10 File Processing

C++ does not, as a part of the language, define how data are sent out and read into the program

C++ Structures Programming Workshop 2 (CSCI 1061U)

Introduction to Standard C++ Console I/O. C++ Object Oriented Programming Pei-yih Ting NTOU CS

Introduction to C++ (Extensions to C)

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

C++ STREAMS; INHERITANCE AS

C++ basics Getting started with, and Data Types.

Week 5: Files and Streams

Input/output. Remember std::ostream? std::istream std::ostream. std::ostream cin std::istream. namespace std { class ostream { /*...

Simple C++ Program. #include <iostream> using namespace std; int main() { //Note cout << Hello World << endl; return 0;

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files.

C++ Input/Output Chapter 4 Topics

PROGRAMMING EXAMPLE: Checking Account Balance

FILE IO AND DATA REPRSENTATION. Problem Solving with Computers-I

Iterative Constructs

basic_fstream<chart, traits> / \ basic_ifstream<chart, traits> basic_ofstream<chart, traits>

Software Design & Programming I

CSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1]

Introduction to Programming EC-105. Lecture 2

UEE1303(1070) S 12 Object-Oriented Programming in C++

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Unit-V File operations

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

Programming in C/C

Lab 6. Review of Variables, Formatting & Loops By: Dr. John Abraham, Professor, UTPA

Fig: iostream class hierarchy

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

IT 1033: Fundamentals of Programming Data types & variables

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 11: Records (structs)

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Chapter 21 - C++ Stream Input/Output

Understanding main() function Input/Output Streams

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Streams. Rupesh Nasre.

UNIT V FILE HANDLING

Fundamentals of Programming CS-110. Lecture 2

System Design and Programming II

Convenient way to deal large quantities of data. Store data permanently (until file is deleted).

Preview 8/28/2018. Review for COSC 120 (File Processing: Reading Data From a File)

Lab: Supplying Inputs to Programs

COMP322 - Introduction to C++

Engineering Problem Solving with C++, Etter/Ingber

CHAPTER 3 BASIC INSTRUCTION OF C++

CS 103 Unit 14 - Streams

Computer Programming : C++

CMPS 221 Sample Final

Lecture 3 The character, string data Types Files

I/O streams

Data Types & Variables

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Ch 6-2. File Input / Output

Transcription:

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 7 Seungkyu Lee Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Input entities Keyboard, files Output entities Monitor, files Standard input keyboard Standard output monitor Chapter 7

Text files Files all the data are stored as characters Binary files Data in the internal computer formats (Remember the binary )

Streams Creating Connecting (source or destination) Disconnecting.

Standard Streams Standard streams are created, connected, and disconnected automatically.

File Streams File streams are created, connected to files, and disconnected from files by the programmer.

File Streams ifstream (for reading from file) ofstream (for writing to file) fstream (read and write) Creating file streams 1. First define stream objects ifstream [stream variable name]; ofstream [stream variable name]; fstream [stream variable name]; 2. Connecting file streams Open () 3. Disconnecting file streams Close ()

Standard Library Input/Output File open ifstream fsinput; Functions (1) fsinput.open( temp.txt ); File close fsinput.close();

Standard Library Input/Output Functions (2) #include <fstream> int main() { ifstream fstemp; fstemp.open( temp.txt ); // process file } fstemp.close(); return 0;

Open and Close Errors #include <iostream> #include <fstream> using namespace std; int main() { } ifstream fsdailytemps; fsdailytemps.open ("ch7temps.dat"); if (!fsdailytemps) { cerr << ERROR 100 opening ch7temps.dat\n"; exit (100); } fsdailytemps.close(); if (fsdailytemps.fail()) { cerr << ERROR 102 closing ch7temps.dat\n"; exit (102); } cout << "End open/close error test\n"; return 0;

Formatting Input and Output Reading from file ifstream fstemp; fstemp.open ( temp.txt"); fstemp >> a; Writing to file ofstream fstemp; fstemp.open ( temp.txt"); fstemp << a; cin >> a; cout << a;

Formatting Input and Output #include <fstream> int main() { ofstream fstemp; fstemp.open( temp.txt ); int a=123; fstemp << a; } fstemp.close(); return 0;

Formatting Input and Output #include <fstream> int main() { ifstream fstemp; fstemp.open( temp.txt ); int a; fstemp >> a; cout << a; } fstemp.close(); return 0;

Formatting Input and Output #include <fstream> int main() { ofstream fstemp; fstemp.open( temp.txt ); char mych= a ; fstemp.put(mych); } fstemp.close(); return 0;

Formatting Input and Output #include <fstream> int main() { ifstream fstemp; fstemp.open("temp.txt"); char mych; while(fstemp.get(mych)) cout << mych; temt.txt 1. abc 2. a b c } fstemp.close(); return 0;

Formatting Input and Output #include <fstream> int main() { ifstream fstemp; fstemp.open("temp.txt"); char mych; for (int i=1;i<6;i++){ fstemp >> mych; cout << mych; } temt.txt 1. abc 2. a b c } fstemp.close(); return 0;

Formatting Data (1) Control variables width: determine how may display positions are to be used to display the data. fill: determines the nondata character that is to print when the print width is greater than the data width. precision: determines the number of digits to be displayed after the decimal point.

Formatting Input and Output #include <fstream> int main() { ofstream fstemp; fstemp.open("temp.txt"); int a=123; double b=12.12345678; fstemp.width(15); fstemp << a << endl; fstemp.width(15); fstemp << b << endl; } fstemp.close(); return 0;

Formatting Input and Output #include <fstream> int main() { ofstream fstemp; fstemp.open("temp.txt"); int a=123; double b=12.12345678; fstemp.width(15); fstemp << a << endl; fstemp.width(15); fstemp.precision(10); fstemp << b << endl; } fstemp.close(); return 0;

Formatting Data (2) code 1/2 #include <iostream> #include <cstdlib> using namespace std; int main() { cout << "Test control variables\n"; cout << "Print with default settings\n"; cout << 'a' << 'B' << 'c' << endl; cout << "Print with width 10\n"; cout.width (10); cout << 'a' << 'B' << 'c' << endl; cout << "\ntest fill character with * char\n"; cout.width(5); cout.fill ('*'); cout << 'a'; cout.width(5); cout << 'B'; cout.width(5); cout << 'c'; cout << "\nresetting default fill char\n"; cout.fill(' '); cout.width(5); cout << 'a' << 'B' << 'c' << endl;

Formatting Data (3) code 2/2 cout << "\ntest precision\n"; cout.setf (ios::fixed, ios::floatfield); cout << "Print without precision\n"; cout << 123.45678 << endl; cout << "Print with precision 0\n"; cout.precision(0); cout << 123.45678 << endl; cout << "Print with precision 3\n"; cout.precision(3); cout << 123.45678 << endl; } cout << "Print without precision\n"; cout << 123.45678 << endl; return 0;

Input/Output Stream Flags Turning on or off input/output settings Turning on: stream.setf(ios::[flagname]); Turning off: stream.unsetf(iso::[flagname])

Stream Flag Examples: skipws

adjustfield, left, right

scientific

Student Grade Example

Student Grade Example

Student Grade Example

Student Grade Example

Creating Text File

Copying Text File

Counting Characters and Lines

Counting Words