1 CS 105 Lecture 9 Files Version of Mon, Mar 28, 2011, 3:13 pm.

Slides:



Advertisements
Similar presentations
Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson.
Advertisements

1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Classes and Objects Objects of class type string Input/Output Objects and Formatted Input/Output 6/30/2015MET CS 563--Fall A. Using Class Type Objects.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson.
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 3: Input/Output
Announcements Quiz 2 Grades Posted on blackboard.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
C++ Streams Lecture-2.
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
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.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
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.
C++ Streams Lecture-2. C++ Streams Stream  A transfer of information in the form of a sequence of bytes I/O Operations:  Input stream: A stream that.
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:
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
COMP102 Lab 071 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Chapter 3: Input/Output
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!"
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
C++ REVIEW INPUT/OUTPUT (I/O). BRIEF NOTE “CLASSES” AND “STRUCTS” AND “TYPES” They are ADTs… They define data and operations on that data The ADTs in.
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.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Chapter 05 (Part II) Control Statements: Part II.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
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.
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3 L7.
Output Stream Formatting
Quiz Next Monday.
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
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.
Today’s Lecture I/O Streams Tools for File I/O
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
File Review Declare the File Stream Object Name
Chapter 3 Input output.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
Chapter 4 INPUT AND OUTPUT OBJECTS
Formatted Input, Output & File Input, Output
Programming File I/O.
Let’s all Repeat Together
CHAPTER 4 File Processing.
Reading from and Writing to Files
Programming with Data Files
C++ Programming Lecture 8 File Processing
Reading from and Writing to Files
Presentation transcript:

1 CS 105 Lecture 9 Files Version of Mon, Mar 28, 2011, 3:13 pm

2 Files Data in main memory is “volatile” File: Place for “permanent” data storage C: drive, A: drive, flash drive, etc. In C++, files can be read different ways Stream input (or output) lets us read (or write) a sequence of data. We also use streams (cin, cout) to model keyboard input and text output.

3 In C++, the programmer can declare output streams and link them to output files. Output File Streams #include using namespace std;.... int num1, num2, num3; cout << "Enter 3 numbers for datafile: "; cin >> num1 >> num2 >> num3; ofstream outputStream; outputStream.open("datafile.txt"); outputStream << num1 << " "; outputStream << num2 << " "; outputStream << num3 << endl; outputStream.close();

4 In C++, the programmer can declare input streams and link them to input files. Input File Streams #include using namespace std;.... int num1, num2, num3; ifstream inputStream; inputStream.open("datafile.txt"); inputStream >> num1 >> num2 >> num3; inputStream.close();

5 To Read or Write from a File Declare the file stream object. I.e., give it a name. Associate a file name with the file stream object by opening the file. Read or write to the file using > operators. Close the file.

6 Manipulators Used to format output neatly: Left justification (columns of names) Right justification (columns of numbers) Minimum field length (padding) Decimal places For more information, Malik pgs There's also an older style of formatting that uses "printf" and "scanf" that has been copied into Java.

7 Example of Output Manipulation #include using namespace std; int main() { ifstream input; int loops, ival, i; float fval; string name; cout << showpoint << scientific; cout << setprecision(2); input.open("mydata.txt"); input >> loops; for (i=0; i < loops; i++) { input >> ival; input >> fval; input >> name; cout << right; cout << setw(4) << ival << " "; cout << setw(12) << fval << " "; cout << left; cout << setw(10) << name << endl; } return(0); } Output e+000 Jon e+001 Bill e+010 Mary e+000 Smith e+003 xyz

8 Example with File Input #include using namespace std; int main() { ifstream input; int loops, ival, i; float fval; string name; cout << showpoint << scientific << setprecision(2); input.open("mydata.txt"); input >> loops; for (i=0; i < loops; i++) { input >> ival; input >> fval; input >> name; cout << right; cout << setw(4) << ival << " "; cout << setw(12) << fval << " "; cout << left; cout << setw(10) << name << endl; } return(0); } mydata.txt file: Jon Bill e9 Mary Smith -3 -4e3 xyz Output: e+00 Jon e+01 Bill e+10 Mary e+00 Smith e+03 xyz