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

Similar documents
C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

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

Introduction to C++ (Extensions to C)

Streams. Rupesh Nasre.

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

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

C++ Input/Output: Streams

C++ Programming Lecture 10 File Processing

Objects and streams and files CS427: Elements of Software Engineering

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

Streams - Object input and output in C++

Chapter 3 - Notes Input/Output

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

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

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

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

Chapter 12 - C++ Stream Input/Output

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

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

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

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

We will exclusively use streams for input and output of data. Intro Programming in C++

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

by Pearson Education, Inc. All Rights Reserved. 2

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

PROGRAMMING EXAMPLE: Checking Account Balance

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

COMP322 - Introduction to C++

Getting started with C++ (Part 2)

Formatting outputs String data type Interactive inputs File manipulators. Access to a library that defines 3. instead, a library provides input

Software Design & Programming I

Pointers, Arrays and C-Strings

Input and Output File (Files and Stream )

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

Object Oriented Programming Using C++ UNIT-3 I/O Streams

What we will learn about this week:

We can even use the operator << to chain the output request as:

Topic 2. Big C++ by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

C++ Quick Guide. Advertisements

Understanding main() function Input/Output Streams

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

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

CS242 COMPUTER PROGRAMMING

CS201 Solved MCQs.

COMP322 - Introduction to C++

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

Chapter 3: Input/Output

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

Chapter 21 - C++ Stream Input/Output

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

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

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

Chapter 14 Sequential Access Files

Chapter 15 - C++ As A "Better C"

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

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Computer Programming : C++

BITG 1113: Files and Stream LECTURE 10

Jan 27, C++ STL Streams. Daniel Maleike

Input/Output Streams: Customizing

Chapter 12. Streams and File I/O

Setting Justification

Sequential Program Execution

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

Arrays. Week 4. Assylbek Jumagaliyev

C++ Input/Output Chapter 4 Topics

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

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

BITG 1233: Introduction to C++

Chapter 21 - C++ Stream Input/Output

Object Oriented Programming In C++

CSCI 1061U Programming Workshop 2. C++ Basics

EP241 Computing Programming

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Chapter 3. Numeric Types, Expressions, and Output

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

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

Streams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing

Chapter 2: Introduction to C++

Scientific Computing

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Lecture 9. Introduction

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.

VuZs Team's Work. CS201 Spring Solved by vuzs Team with Reference Written by Administrator Wednesday, 19 May :52

Consider the following example where a base class has been derived by other two classes:

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

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

IS 0020 Program Design and Software Tools

Unit 1 : Principles of object oriented programming

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

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-

Introduction to Programming EC-105. Lecture 2

Fundamentals of Programming Session 27

LECTURE 02 INTRODUCTION TO C++

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

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

Fundamentals of Programming CS-110. Lecture 2

Console input 26/09/2018. Background. Background

Transcription:

Input and Output Data Processing Course, I. Hrivnacova, IPN Orsay Output to the Screen Input from the Keyboard IO Headers Output to a File Input from a File Formatting I. Hrivnacova @ Data Processing Course 2018 1

C++ Program /* The first C++ program * - just outputs 'Hello, World!' */ int main() // print Hello, World! on the screen cout << Hello, World! << endl; Including the necessary declarations for input and output Writing the string Hello, World! followed by std::endl symbol, to the standard output I. Hrivnacova @ Data Processing Course 2018 2

Input and Output (IO) Chua Hock-Chuan: Programming Notes C/C++ IO are based on streams, which are sequence of bytes flowing in and out of the programs (just like water and oil flowing through a pipe). In input operations, data bytes flow from an input source (such as keyboard, file, network or another program) into the program. In output operations, data bytes flow from the program to an output sink (such as console, file, network or another program). I. Hrivnacova @ Data Processing Course 2018 3

Input and Output The input/output (I/O) functionality is provided in components of standard library: Standard channels to provide input/output: Generally assigned to the keyboard (input) and the screen (output) Can be redirected by the operating system to a file The standard library defines types and objects specially designed for IO Objects: cin, cout - input channel (the keyboard), output channel (the screen) Global stream objects, they are already declared, and so available for use Types: istream, ostream : defines the type for objects that can be used for input, output Operators: <<, >> operator: Send, receive data t/fromo the output/an input channel I. Hrivnacova @ Data Processing Course 2018 4

Output to the Screen We use the stream std::cout and the operator << #include <string> int main () int i = 1; float f = 2.3; string s = "abcd"; cout << " i=" << i << " f=" << f << " s=" << s << std::endl; The operator << is intelligent: it adapts to the type of the object to its right I. Hrivnacova @ Data Processing Course 2018 5

Output to the Screen We use the stream std::cout and the operator << #include <string> int main () int i = 1; float f = 2.3; string s = "abcd"; cout << " i=" << i << " f=" << f << " s=" << s << std::endl; Program output: >./testoutput i=1 f=2.3 s=abcd I. Hrivnacova @ Data Processing Course 2018 6

Input from the Keyboard We use the stream std::cin and the operator >> #include <string> int main () int i; float f; string s; cin >> i >> f >> s; cout << " i=" << i << " f=" << f << " s=" << s << endl; The entries in cascade can be separated by any separation character: space, TAB, CR I. Hrivnacova @ Data Processing Course 2018 7

Input from the Keyboard We use the stream std::cin and the operator >> #include <string> int main () int i; float f; string s; cin >> i >> f >> s; cout << " i=" << i << " f=" << f << " s=" << s << endl; Program output: >./testinput 1 2 Blablah i=1 f=2 s=blablah I. Hrivnacova @ Data Processing Course 2018 8

IO Header Files Header File <iostream> <fstream> <iomanip> Function and Description This file defines the cin, cout, cerr and clog objects, which correspond to the standard input stream, the standard output stream, the un-buffered and buffered standard error stream, respectively. This file declares services for user-controlled file processing. This file declares services useful for performing formatted I/O with so-called parameterized stream manipulators, such as setw and setprecision. I. Hrivnacova @ Data Processing Course 2018 9

Files The file input/output (I/O) functionality is provided in components of standard library: #include <fstream> Types: ifstream, ofstream : define the types for objects that can be used for input, output They are of the same family as istream and ostream, and so the objects of this type can be manipulated the same way Using operators: <<,>> Objects There are no global file stream objects (such as cin and cout), we have to define them ourselves before we can use them We choose the names of our stream objects ifstream infile("input.txt"); ofstream outfile("output.txt"); I. Hrivnacova @ Data Processing Course 2018 10

#include <fstream> #include <string> int main () int i = 1; float f = 2.3; string s = "abcd"; ofstream outfile("file.txt"); if (! outfile.is_open() ) cerr << "Cannot open file" << endl; return 1; outfile << i << " " << f << " " << s << std::endl; outfile.close(); Output to a File The operation to open a file may fail If the output file does not exist, if one has no right to write to the directory for the output file... We have to test the result of the open operation: The operator << is used in the same way as to output to the screen I. Hrivnacova @ Data Processing Course 2018 11

#include <fstream> #include <string> int main () int i = 1; float f = 2.3; string s = "abcd"; Output to a File ofstream outfile("file.txt"); if (! outfile.is_open() ) cerr << "Cannot open file" << endl; return 1; outfile << i << " " << f << " " << s << std::endl; outfile.close(); Program output: >./testfileoutput > more file.txt 1 2.3 abcd I. Hrivnacova @ Data Processing Course 2018 12

#include <fstream> #include <string> using namespace srd; int main () int i; float f; std::string s; ifstream infile("file.txt"); if (! infile.is_open() ) cerr << "Cannot open file" << endl; return 1; infile >> i >> f >> s; Input from a File The operation to open a file may fail The input file may not exist We have to test the result of the open operation: The operator >> is used in the same way as to input from the keyboard std::cout << " i=" << i << " f=" << f << " s=" << s << std::endl; I. Hrivnacova @ Data Processing Course 2018 13

#include <fstream> #include <string> using namespace srd; Input from a File int main () int i; float f; std::string s; ifstream infile("file.txt"); if (! infile.is_open() ) cerr << "Cannot open file" << endl; return 1; infile >> i >> f >> s; Program output: >./testfileinput std::cout << " i=" << i i=1 f=2.3 s=abcd << " f=" << f << " s=" << s << std::endl; I. Hrivnacova @ Data Processing Course 2018 14

Formatting Output The <iomanip> header provides so-called I/O manipulators for formatting output (and input): setw(int field-width) setfill(char fill-char) left right internal fixed scientific setprecision(int numdecimaldigits) boolalpha noboolalpha Set the field width for the next IO operation. setw() is non-sticky and must be issued prior to each IO operation. Set the filled character for padding to the field width. left right internal: set the alignment. Set the alignment. Use fixed-point notation (e.g, 12.34) or scientific notation (e.g., 1.23e+006). For floating-point numbers. Specify the number of digits after the decimal point. For floating-point numbers. display bool values as alphabetic string (true/false) or 1/0. For bool. I. Hrivnacova @ Data Processing Course 2018 15

#include <iomanip> // Needed to do formatted I/O Formatting Output int main() // Floating point numbers double pi = 3.14159265; cout << fixed << setprecision(4); // fixed format with 4 decimal places cout << pi << endl; cout << " " << setw(8) << pi << " " << setw(10) << pi << " " << endl; // setw() is not sticky, only apply to the next operation. cout << setfill('-'); cout << " " << setw(8) << pi << " " << setw(10) << pi << " " << endl; cout << scientific; // in scientific format with exponent cout << pi << endl; // booleans bool done = false; cout << done << endl; // print 0 (for false) or 1 (for true) cout << boolalpha; // print true or false cout << done << endl; I. Hrivnacova @ Data Processing Course 2018 16

#include <iomanip> // Needed to do formatted I/O Formatting Output int main() // Floating point numbers double pi = 3.14159265; cout << fixed << setprecision(4); // fixed format with 4 decimal places cout << pi << endl; cout << " " << setw(8) << pi << " " << setw(10) << pi << " " << endl; // setw() is not sticky, only apply to the next operation. cout << setfill('-'); cout << " " << setw(8) << pi << " " << setw(10) << pi << " " << endl; cout << scientific; // in scientific format with exponent cout << pi << endl; Program output: >./testformatting 3.1416 // booleans bool done = false; 3.1416 3.1416 cout << done << endl; // print 0 (for false) or 1 (for --3.1416 ----3.1416 true) cout << boolalpha; // print true or false 3.1416e+00 cout << done << endl; 0 false I. Hrivnacova @ Data Processing Course 2018 17