Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included for string manipulation? how is.

Slides:



Advertisements
Similar presentations
Getting Data into Your Program
Advertisements

L which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? l how is a string assigned.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
1 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
Chapter 12 Streams and File I/O Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 12 Streams and File I/O. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  I/O Streams  File I/O  Character.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
Chapter 12 Streams and File I/O Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 12 Streams and File I/O. Learning Objectives I/O Streams – File I/O – Character I/O Tools for Stream I/O – File names as input – Formatting output,
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
File I/O ifstreams and ofstreams Sections 11.1 &
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
I/O in C++ October 7, Junaed Sattar. Stream I/O a stream is a flow of bytes/characters/ints or any type of data input streams: to the program output.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
Lecture 14 Arguments, Classes and Files. Arguments.
Files To read a file – We must know its name – We must open it (for reading) – Then we can read – Then we must close it That is typically done implicitly.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
Copyright © 2002 Pearson Education, Inc. Slide 1.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
File I/O. Files allow permanent storage of programs and data. ifstream and ofstream objects –For file I/O the inclusion of is required. –Objects for file.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
Strings.
CS212: Object Oriented Analysis and Design
ifstreams and ofstreams
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CS Computer Science IA: Procedural Programming
CS-103 COMPUTER PROGRAMMING
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? how is.
FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by
Data Streams.
Standard Input/Output Streams
Standard Input/Output Streams
File I/O.
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Standard Input/Output Stream
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
File I/O in C++ I.
CS150 Introduction to Computer Science 1
File I/O.
CS 144 Advanced C++ Programming February 5 Class Meeting
Reading from and Writing to Files
ifstreams and ofstreams
character manipulation
Lecture 3 More on Flow Control, More on Functions,
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included for string manipulation? how is a string assigned a value? Can a string be initialized at declaration? how is getline() different from the extraction operator? what does this line do? string s1=”hi” + ’ ’ + ”there”; how are strings compared? Which one is greater? hell bell hello how can the size of a string be determined? Can string size be changed? What is the size of this string? string s2=”hello”; is there a problem with this statement? cout << s2[4]; assuming string s3=”C++ is fun”; what do the following functions do? s3.substr(4,2); s3.find(’+’); s3.rfind(’+’); s3.find_first_of(”is”); s3.find_last_of(”is”); s3.insert(3,2,’ ’); s3.replace(1,3,”--”); s3.append(”!!”); s3.erase(0,3);

Input/Output 2

Streams C++ uses streams for input and output stream - is a sequence of data to be read (input stream) or a sequence of data generated by the program to be output (output stream) by default only standard streams are declared: cin (console input) cout (console output) files are used to store information on permanent basis to do file input/output <fstream> needs to be included as well as using std::ifstream; and using std::ofstream; or using namespace std; an input or output stream needs to be declared: ifstream in_stream; // input stream ofstream out_stream; // output stream before C++ program can read from or write to a file stream, the stream needs to be connected to a file. This is referred to as opening the file: in_stream.open(”myfile.dat”); // opening file or combined: ifstream instream(“myfile.dat”); // Open myfile

Streams (Cont.) a string variable can be used as file name c_str() function has to be applied to the string: string filename=”myfile.dat”; instream.open(filename.c_str()); after stream variable has been connected to a file, extraction and insertion operators can be used with a stream: in_stream >> var1 >> var2 >> var3; after the file is opened all the operations on the file is done through associated stream before program finishes every stream needs to closed: in_stream.close(); // closing file one file cannot be opened for both reading and writing (even if two different streams are used) one file can be opened for reading, closed and then opened for writing or v.v. function eof() returns true if end of input file has been reached: while(!in_stream.eof()){ … }

I/O Error Checking function fail() returns true if previous stream operation failed Useful for checking reads and writes for problems, like data errors, end of data, formatting errors, etc. Can also be used to check whether an open operation succeeded before performing operations on the file. useful function exit() - exists program immediately. To use exit, include <cstdlib> exit() accepts one integer as argument: convention: 0 - normal program termination; nonzero - abnormal Example in_stream.open(”myfile.dat”); // opening file if (in_stream.fail()){ cout << ”Cannot open file myfile.dat”; exit(1); }

Streams as Parameters void myfunc(ifstream &myinfile); streams can be passed as parameters streams must be passed by reference void myfunc(ifstream &myinfile);