EP241 Computing Programming

Similar documents
Programming II with C++ (CSNB244) Lab 10. Topics: Files and Stream

Input and Output File (Files and Stream )

Objects and streams and files CS427: Elements of Software Engineering

C++ Programming Lecture 10 File Processing

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

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

BITG 1113: Files and Stream LECTURE 10

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

System Design and Programming II

C++ Binary File I/O. C++ file input and output are typically achieved by using an object of one of the following classes:

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

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

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

C++ Input/Output: Streams

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

Fall 2017 CISC/CMPE320 9/27/2017

Input/Output Streams: Customizing

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

Ch 6-2. File Input / Output

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

COMP322 - Introduction to C++

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

Object Oriented Programming In C++

Chapter 14 Sequential Access Files

Chapter 11 Customizing I/O

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

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

Chapter 11 Customizing I/O

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

Getting started with C++ (Part 2)

Streams - Object input and output in C++

Module 11 The C++ I/O System

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

Fig: iostream class hierarchy

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

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

IS 0020 Program Design and Software Tools

Fundamentals of Programming Session 25

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

Developed By : Ms. K. M. Sanghavi

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

COMP322 - Introduction to C++

Streams in C++ Stream concept. Reference information. Stream type declarations

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

Chapter 11 Customizing I/O

Streams contd. Text: Chapter12, Big C++

Setting Justification

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

Chapter 12. Streams and File I/O

EP241 Computing Programming

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

C++ files and streams. Lec 28-31

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.

CS490: Problem Solving in Computer Science Lecture 3: Input/Output

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

Advanced I/O Concepts

More File IO. CIS 15 : Spring 2007

Chapter 12 - C++ Stream Input/Output

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

What we will learn about this week:

Lecture 9. Introduction

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

Chapter 8 File Processing

CS201 Solved MCQs.

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

Unit-V File operations

Fundamentals of Programming Session 27

Chapter-12 DATA FILE HANDLING

UNIT V FILE HANDLING

Chapte t r r 9

After going through this lesson, you would be able to: store data in a file. access data record by record from the file. move pointer within the file

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

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

Lecture 4 Tao Wang 1

Random File Access. 1. Random File Access

Window s Visual Studio Output

Lecture 14. Xiaoguang Wang. March 11th, 2014 STAT 598W. (STAT 598W) Lecture 14 1 / 36

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

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

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

Computational Physics

Chapter 3 - Notes Input/Output

READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties. Do not start the test until instructed to do so!

PROGRAMMING EXAMPLE: Checking Account Balance

C++ for Python Programmers

EEE145 Computer Programming

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

Generate error the C++ way

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

BITG 1233: Introduction to C++

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

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

Study Material for Class XII. Data File Handling

Introduction to C++ (Extensions to C)

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

PASS$MOCK$EXAM$ $FOR$PRACTICE$ONLY$

by Pearson Education, Inc. All Rights Reserved. 2

TEST 1 CS 1410 Intro. To Computer Science Name KEY. October 13, 2010 Fall 2010

Chapter 3. Numeric Types, Expressions, and Output

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

Transcription:

EP241 Computing Programming Topic 9 File Management Department of Engineering Physics University of Gaziantep Course web page www.gantep.edu.tr/~bingul/ep241 Sep 2013 Sayfa 1

Overview of Streams in C++ The standard C++ library provides the following classes to perform I/O operations: iostream Stream class for basic I/O (without files). ofstream Stream class to output to files ifstream Stream class to input from files data streams fstream Stream class to both read and write from/to files. Sayfa 2

Format Manipulators In C++ there are different ways to format input and output. In this course we will only use format manipulators. These are objects that are placed in the data stream to change the characteristics of the input or output. For example: double x = 123.4567890123; cout << x << endl; cout << fixed << setprecision(9) << x << endl; cout << scientific << setprecision(7) << x << endl; outputs: 123.457 Default format. 123.456789012 Fixed format, with 9 decimal places. 1.2345679e+02 Scientific format with 7 decimal places. Note: to use manipulators you need to include the <iomanip> header. Sayfa 3

Some commonly used manipulators. Requires #include <iomanip> * setw(n) Sets the width of the next input/output to n. setfill(c) Fills spaces in a number with the character c. * setprecision(n) Display the number with n decimal places. * fixed Display values in fixed-point notation. * scientific Display values in scientific notation. left right internal Left-justify. Right-justify. Left-justify the sign, right-justify the value. * dec Display integer values in decimal format. * oct Display integer values in octal (base 8) format. * hex Display integer values in hexadecimal (base 16). boolalpha noboolalpha Display boolean as true or false. Display boolean as 1 or 0 (default). Sayfa 4

Formatted Output Manipulators are injected into the output stream to control the format of following streams of data. In general we combine manipulators to obtain the desired results: #include <iomanip> int main() { double x = 123.456; cout << setw(10) << fixed << setprecision(2) << x; cout << setw(15) << scientific << setprecision(3) << x; Output 123.46 1.235e+002 Column 1234567890123456789012345 Sayfa 5

File Input/Output Reading from and writing to files is performed using file stream classes. ifstream ofstream is used for reading (inputting) data from a file. is used for writing (outputting) data to a file. These two class require the <fstream> header to be included. File processing requires some basic actions: - Stating the name of the file. - Opening an existing file, or created a new one. - Optionally stating the read/write mode. - Optionally determining if a file is successfully open. - Detecting the end of a file (when reading). - Closing a file after is has been used. These actions are provided by fstream functions Sayfa 6

Some functions of fstream Function open( filename, mode) Description filename is the name (and path) of the file to open. mode is an optional parameter and can have the following flags: ios::in open for input operations (default for ifstream). ios::out open for output operations (default for ofstream). ios::binary open in binary mode (default is text mode). ios::ate set the initial position at the end of the file. (default is the beginning of the file). ios::app append the content to the current content of the file. ios::trunc delete the previous content and replace with new..is_open() Returns true if a file is successfully opened..eof() Returns true if a file open (for reading) has reached the end..close() Closes the file. Sayfa 7

Writing data to a file called numbers.txt #include <fstream> int main() { int i; ofstream dosya("numbers.txt"); for (i=1; i<=10; i++){ dosya << i << " " << i*i << endl; dosya.close(); The file numbers.txt contains: 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100 Sayfa 8

Writing data to a file called inverse.txt #include <fstream> #include <iomanip> int main() { ofstream dosya("inverse.txt"); dosya <<setprecision(2) << scientific; for (int i=1; i<=10; i++){ double x = 1.0/i; dosya << setw(3) << i << setw(10) << x << endl; dosya.close(); The file inverse.txt contains: 1 1.00e+00 2 5.00e-01 3 3.33e-01 4 2.50e-01 5 2.00e-01 6 1.67e-01 7 1.43e-01 8 1.25e-01 9 1.11e-01 10 1.00e-01 Sayfa 9

Writing data to a file called data.txt #include <fstream> int main() { double a[4] = {8.4, 3.6, 9.1, 4.7; ofstream myfile("data.txt"); for (int i=0; i<4; i++) myfile << a[i] << endl; The file data.txt contains: 8.4 3.6 9.1 4.7 myfile.close(); Sayfa 10

Writing data to a file called data.txt #include <fstream> int main() { double a[4] = {8.4, 3.6, 9.1, 4.7; ofstream myfile("data.txt"); if ( myfile.is_open() ) { for (int i=0; i<4; i++) myfile << a[i] << endl; The file data.txt contains: 8.4 3.6 9.1 4.7 myfile.close(); else cout << "Unable to open file!"; Sayfa 11

Reading data from a file called data.txt #include <fstream> int main() { double a[4]; ifstream myfile("data.txt"); for (int i=0; i<4; i++) { myfile >> a[i]; cout << "a[" << i << "]=" << a[i] << endl; myfile.close(); Given that the file data.txt contains: 8.4 3.6 9.1 4.7 The output is a[0]=8.4 a[1]=3.6 a[2]=9.1 a[3]=4.7 Sayfa 12

Reading data from a file called data.txt #include <fstream> int main() { double a[4]; ifstream myfile("data.txt"); if ( myfile.is_open() ) { for (int i=0; i<4; i++) { myfile >> a[i]; cout << "a[" << i << "]=" << a[i] << endl; myfile.close(); else cout << "Unable to open file!"; Given that the file data.txt contains: 8.4 3.6 9.1 4.7 The output is a[0]=8.4 a[1]=3.6 a[2]=9.1 a[3]=4.7 Sayfa 13

Reading data from a file called student.dat #include <fstream> #include <string> int main() { const int n = 10; int score[n]; string name[n]; double sum = 0.0; ifstream myfile("student.dat"); for (int i=0; i<n; i++) { myfile >> name[i] >> score[i]; sum = sum + score[i]; myfile.close(); cout << "mean of the class = " << sum / n << endl; Given that the file student.dat contains: UGUR 67 KADIR 72 SEMIH 00 GOZDE 73 FIRAT 30 EDA 51 FULYA 41 GUL 67 EMRE 30 AHMET 76 The output is mean of the class = 50.7 Sayfa 14

Example: reading periodic table data from a file atoms.txt 2 He Helium 4.002602 NobleGas 3 Li Lithium 6.941 AlkaliMetal 4 Be Beryllium 9.012182 AlkalineEarth 5 B Boron 10.811 Metaloid 6 C Carbon 12.0107 NonMetal 7 N Nitrogen 14.00674 NonMetal 8 O Oxygen 15.9994 NonMetal 9 F Fluorine 18.998404 Halogen 11 Na Sodium 22.9898 AlkaliMetal 12 Mg Megnesium 24.305 AlkalineEarth For each atom, each property is separated by a single space. We call this a space-delimited list of values, and reading such lists is simple in C++ (see next page). Sayfa 15

Example: reading periodic table data from a file #include <fstream> int main () { const int n = 10; // read 10 atom data int number[n]; string symbol[n], name[n], type[n]; double mass[n]; ifstream atoms("atoms.txt"); for (int i=0; i<n; i++) atoms >> number[i] >> symbol[i] >> name[i] >> mass[i] >> type[i]; atoms.close();.. Now use the arrays The format of the data is: number (int ) symbol (string) name (string) mass (double) type (string) Here we are assuming that we have exactly n=10 atoms in the file! Sayfa 16

#include <fstream> #include <vector> int main () { vector<int> number; vector<string> symbol, name, type; vector<double> mass; ifstream atoms("atoms.txt"); int z; string s, n, t; double m; while(true) { atoms >> z >> s >> n >> m >> t; if ( atoms.eof() ) break; number.push_back(z); symbol.push_back(s); name.push_back(n); mass.push_back(m); type.push_back(t); atoms.close();.. Now use the vector arrays If we don t know how many atoms there are in the file, then we can use vectors with the.push_back method, and the.eof method to detect the end of the file. Note that we need scalars z, s, n, m and t to read values from the file and insert them into the vectors. The file atoms.txt can now be expanded to incorporate more atoms without changing the program that reads the data. Sayfa 17

Homework Please see: http://www1.gantep.edu.tr/~bingul/ep241/homework.php Sayfa 18