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.

Slides:



Advertisements
Similar presentations
CPS120: Introduction to Computer Science INPUT/OUTPUT.
Advertisements

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.
File streams Chapter , ,
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Chapter 3: Input/Output
Program Input and the Software Design Process ROBERT REAVES.
Chapter 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
CHAPTER 3 INPUT/OUTPUT. In this chapter, you will:  Learn what a stream is and examine input and output streams  Explore how to read data from the standard.
1 Chapter 4 Program Input and the Software Design Process.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
File I/O ifstreams and ofstreams Sections 11.1 &
1 CS161 Introduction to Computer Science Topic #13.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Program Input and the Software Design Process ROBERT REAVES.
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
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.
1 Chapter 4 Program Input and the Software Design Process.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
24 4/11/98 CSE 143 Stream I/O [Appendix C]. 25 4/11/98 Input/Output Concepts  Concepts should be review!  New syntax, but same fundamental concepts.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Expressions and Interactivity.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Chapter 4 Program Input and the Software Design Process
Topic 2 Input/Output.
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3: Expressions and Interactivity.
Input and Output Chapter 3.
File I/O.
Standard Input/Output Streams
Standard Input/Output Streams
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.
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 3 Input output.
Chapter 3: Expressions and Interactivity
Chapter 4 Program Input and the Software Design Process
CS150 Introduction to Computer Science 1
Chapter 4 INPUT AND OUTPUT OBJECTS
Formatted Input, Output & File Input, Output
CS150 Introduction to Computer Science 1
CPS120: Introduction to Computer Science
Reading from and Writing to Files
Reading Data From and Writing Data To Text Files
Chapter 1 c++ structure C++ Input / Output
getline() function with companion ignore()
Reading from and Writing to Files
Presentation transcript:

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 operator (>>) skips any leading white space (spaces, tabs, newlines) characters before reading the data. User Input 45 67\n 3 8\n int num1, num2, num3, num4; cin >> num1; cin >> num2; cin >> num3; cin >> num4; num1 = 45 num2 = 67 num3 = 3 num4 = 8

CIN / COUT REVIEW  If you want to skip past ALL remaining data (not just white space) on a line and go to the next line, you can use cin.ignore(). User Input 45 67\n 3 8\n int num1, num2; cin >> num1; cin.ignore(100, ‘\n’); cin >> num2; num1 = 45 skips remaining data on line 1 num2 = 2

CIN / COUT REVIEW  If you want to read all characters (includig whitespace characters instead of skipping past them) you can use the cin.get() to read into char type variables. User Input 4\n 3 8\n char ch1, ch2, ch3, ch4, ch5; cin.get(ch1); cin.get(ch2); cin.get(ch3); cin.get(ch4); cin.get(ch5); ch1 = ‘4’ ch2 = ‘\n’ ch3 = ‘3’ ch4 = ‘ ‘ ch5 = ‘8’

CIN / COUT REVIW  To display output to the screen we used “cout”.  The cursor remains where the last output occurs unless “endl” or “\n” is added ( this allows more output to be added to same line). CODE cout << “Hello”; cout << “Hello” << endl; cout << “Good”; cout << “bye” OUTPUT Hello_ Hello _ Goodbye_

CIN / COUT REVIEW  Output can be formatted using manipulators:  fixedforces floating point number to be displayed in a decimal format (instead of scientific)  setw()sets the display with of output data  setprecision()sets number of decimal places to be displayed int num = 456; float val = ; cout << fixed << showpoint << setprecision(2); cout << val; cout << setprecision(1) << val; cout << setw(4) << val; cout << setw(1) << num; cout << setw(5) << num; OUTPUT _ __456

CIN / COUT REVIEW  “cin” will read data according to the data type of input variable  Reading strings stops at the first whitespace character  Using “getline()” forces all characters on line to be read Input data: “\n” int num; float value; char ch; string words cin >> value >> ch; cin >> ch >> num; cin >> words >> num >> value; getline(cin, words); cin >> ch >> ch >> value >> words; value = 435.0, ch = ‘8’ ch = ‘4’, num = 35 words = “435”, num = 8, value = 0.8 words = “ ” ch = ‘3”, value = 5.0, words = “8.8”

TEXT FILES  Text File  File Of ASCII Characters  Can Be Viewed In Text Editor  Last Character On Line Is “newline” Character (‘/n’)  Last Character In The File Is “eof” Character  Can Be Created With Any Text Editor (notepad, compiler…)  Input File Stream  Stream Of Data  Same As Keyboard Input But From The File  Input File Stream Can Be Tricky

TEXT FILES  File Examples character (or string) Data : This Is A Test. This Is Only A Test.(‘/n’) Now Is The Time For All Good Men(‘/n) To Come To The Aid Of Their Country.(‘/n’) (eof) Mixed Data: Smith Joe (‘/n) Able Sam (‘/n) Blow Joe (eof)

USING TEXT FILES  Five Steps For File Access  # 1 – Add “#include ” To Top Of Program  Adds Library For File Access  # 2 – Declare File Stream Variable  Use Valid Identifier Name  Separate File Stream Variables are Required For Text Files Which are Dependant On the Mode (“read” or “write”) –For Input Stream ifstream Identifier - example: ifstream inData; –For Output Stream ofstream Identifier - example:ofstream outData;

USING TEXT FILES  Five Steps For File Access (cont.)  #3 – Open The File Stream  Use The “open” Function file-stream-variable.open(physical file name) example:inData.open(“c:\test.txt); example:const string FILENAME = “test.txt”; outData.open(FILENAME.c_str()); Note:You may need to list all backslashes as double backslashes in file names (e.g. “c:\\study.txt”). Note:file-stream-variable must be of type ifstream or ofstream physical filenames can be a literal string or a C string variable (must append “.c_str()” onto the variable name to convert it to a C string – and be sure to include the “string” header file).

USING TEXT FILES  Five Steps For File Access (cont.)  Potential File “Path” Issues  Opening Input File Stream (read only)  Checks For File Existence If Exists File Opened, Read Pointer Placed At Beginning and File Stream In Non-Error State If MissingFile Stream Is Put Into Error State  Opening Output File Stream (write only)  Checks For File Existence If ExistsFile Opened, All Data Erased, File Ready For Data If MissingNew File Opened, File Ready For Data Note – Text Files Can Only Be Open In One Mode At A Time (Read or Write)

USING TEXT FILES  Five Steps For File Access (cont.)  File Stream Status Can Be Used For Error Checking After opening file “figures.txt” for reading, use the file stream variable to check the results. inData.open("figures.txt"); if ( !inData ) cout << "Error opening file!" << endl; else { // continue program } Result:Error message is issued if program was not able to open file figures.txt.

USING TEXT FILES  Five Steps For File Access (cont.)  #4 – Read Or Write Data File  Similar To Standard Input/Output Process  Instead Of Using “cin” Use File Stream Variable –File “study.txt” contains: (‘\n’) (‘\n’) –Code:double num1; int num2; ifstream inData; ofstream outData; inData >> num1;\\Reads First Number inData >> num2;\\Reads Second Number results:num1 = 35.75, num2 = 47

USING TEXT FILES  Five Steps For File Access (cont.)  Instead Of Using “cout” Use File Stream Variable  Use Of “manipulators” For Formatting Data Has The Same Affect On Output File As They Do With Displayed Output (..setprecision(), setw()…etc) double num1, num2; –Code:num1 = ; num2 = num1 * 2; ( ) outData << fixed << showpoint << setprecision(2); outData << setw(6) << num1 << ‘ ‘ << set(6) << num2 << endl; –Result:_53.76 _106.32(\n’)

USING TEXT FILES  Five Steps For File Access (cont.)  #5 – Close The Open File When Finished  As Soon As File Access Is Completed, Files Should Be Closed inData.close(); outData.close();  Leaving Files Open When Not Needed Increases Opportunity Of Corrupted Files

Using Text Files  Five Steps  Include “fstream” Header File At Top Of Program  Declare File Stream Variables (ifstream or ofstream)  Open The Data File  Use File Stream Variable To Access File (not cin/cout)  Close Any Open Files When Finished With File  Passing The File Stream To A User Defined Function Must Always Be Passed By Reference syntax  For example: void readFile(ifstream& inData) (function header) void readFile(ifstream& inData); (prototype)