計算機概論實習 2007-06-08. 2 Review of Practice 8 (P8) PLEASE write a program that when you fail to open a file, you have to throw a exception. Directly using.

Slides:



Advertisements
Similar presentations
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Advertisements

Passing Streams to Functions. Passing Streams to Functions One Rule: always pass a stream as a reference.
1 CSC 222: Computer Programming II Spring 2004  vectors  library, templated  advantages over arrays:  const-reference parameters  growable vectors.
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.
Strings & Text File Input CIS Feb-06. Quiz 1.Write a function Prototype for the function swap_values that takes two integers by reference and does.
Computer Skills2 for Scientific Colleges 1 File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
計算機概論實習 Files and Streams C++ views file as sequence of bytes Ends with end-of-file marker n-1 end-of-file marker 67 This is.
計算機概論實習 What is Class In C++, class is the keyword which means encapsulation. Property and method are used to define what is class. Materialize.
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
計算機概論實習 How to Use string Template class basic_string String manipulation (copying, searching, etc.) typedef basic_string string; Also typedef.
計算機概論實習 Claim a Parameter int a; a = 5; typenameaddress inta0x0001 addressvalue 0x0001 0x0002 0x0003 0x0004 0x0005 0x0006 0x0007 0x0008.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
計算機概論實習 Stream Stream: sequence of bytes Input: from device (keyboard, disk drive) to memory Output: from memory to device (monitor, printer,
計算機概論實習 Last Practice (P4) members StudentTeacher private: char name[80]; protected: int number; public: void setName(char *); void getName(char*);
1 Lab Session-VIII CSIT-121 Fall 2000 w Formatted Output w Call by Reference w Lab Exercises w File Handling w Lab Exercises.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
計算機概論實習 A Simple Way to Handle Error if(b != 0) { cout
1 Lab Session-VIII CSIT-121 Spring 2002 w Formatted Output w Call by Reference w Lab Exercises w File Handling w Lab Exercises.
計算機程式語言 Lecture 8-1 國立臺灣大學生物機電系 8 8 I/O File Streams and Data Files.
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
計算機程式語言 Lecture 09-1 國立臺灣大學生物機電系 9 9 Completing the Basics.
February 11, 2005 More Pointers Dynamic Memory Allocation.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
ITEC 320 C++ Examples.
CSC 270 – Survey of Programming Languages C++ Lecture 6 – Exceptions.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
Computer Programming TCP1224 Chapter 13 Sequential File Access.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
LECTURE LECTURE 14 Exception Handling Textbook p
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
Lecture 14 Arguments, Classes and Files. Arguments.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
Exception Handling How to handle the runtime errors.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
1 Huffman Codes Using Binary Files. 2 Getting Started Last class we extended a program to create a Huffman code and permit the user to encode and decode.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Topics: Templates Exceptions
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
הרצאה 03 אבני היסוד של תוכנית ב- C
when need to keep permanently
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.
Standard Input/Output Stream
CSC 270 – Survey of Programming Languages
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Statements and flow control
CHAPTER 4 File Processing.
Reading from and Writing to Files
File I/O in C++ II.
Chapter 1 c++ structure C++ Input / Output
Reading from and Writing to Files Part 2
Reading from and Writing to Files
Presentation transcript:

計算機概論實習

2 Review of Practice 8 (P8) PLEASE write a program that when you fail to open a file, you have to throw a exception. Directly using try, catch, and throw Define a openfile() function that will throw exception when this function occurs "can not open a file"

3 A Sample Code #include #include // exit prototype #include using namespace std; void openfile(ifstream &inClientFile, char* filename){ inClientFile.open( filename, ios::in ); if ( !inClientFile ) { throw "File could not be opened"; }

4 void main(){ ifstream inClientFile; char filename[80]; cin >> filename; try{ openfile(inClientFile, filename); } catch(const char* e){ cout << e; exit(1); } char cont[30]; while ( inClientFile >> cont ) cout << cont << endl; inClientFile.close(); }

5 Some Notations!!! (1/2) void openfile(ifstream &inClientFile, string filename){ inClientFile.open( filename, ios::in ); if ( !inClientFile ) { throw "File could not be opened"; } void main(){ ifstream inClientFile; string filename; cin >> filename; try{ openfile(inClientFile, filename); } : } Error!! 無法將參數 1 從 'std::string' 轉換成 'const char *' 改成 filename.data()

6 Some Notations!!! (2/2) void openfile(ifstream &inClientFile, char* filename){ inClientFile.open( filename, ios::in ); if ( !inClientFile ) { throw "File could not be opened"; } void main(){ : catch(string e){ cout << e; exit(1); } : } Compile is OK, but… 改成 string("File could not be opened");

7 Try it!!! Please write a problem that ask user to input a integer number Please use try, catch, throw way to design a function for checking if the input is a integer. Please ask user to re-input if the input is incorrect.