Strings & Text File Input CIS 230 15-Feb-06. Quiz 1.Write a function Prototype for the function swap_values that takes two integers by reference and does.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
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.
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
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.
1 DOS vs. UNIX files Ending lines with “\r\n” vs. “\n” Reading an entire line at a time getline() To skip white space or not cin >> ch; vs. ch = cin.get();
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
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.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
Chapter 3: Input/Output
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
計算機程式語言 Lecture 09-1 國立臺灣大學生物機電系 9 9 Completing the Basics.
1 Chapter 4 Program Input and the Software Design Process.
1 Chapter 4 Program Input and the Software Design Process.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
File I/O ifstreams and ofstreams Sections 11.1 &
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
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.
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.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Program Input and the Software Design Process. 2 Chapter 4 Topics  Input Statements to Read Values for a Program using >>, and functions get, ignore,
1 Character Strings (Cstrings) Reference: CS215 textbook pages
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Chapter 4 Program Input and the Software Design Process.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Functions & Strings CIS Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer.
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.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
1 Chapter 4 Program Input and the Software Design Process.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
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.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Eight: Streams Slides by Evan Gallagher.
Chapter 4 Program Input and the Software Design Process
Topic 2 Input/Output.
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
What Actions Do We Have Part 1
Quiz Next Monday.
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.
Today’s Lecture I/O Streams Tools for File I/O
Chapter 9 File Streams Computing Fundamentals with C++ 3rd Edition
Chapter 3: Input/Output
Standard Input/Output Stream
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Chapter 3 Input output.
Chapter 4 Program Input and the Software Design Process
CS150 Introduction to Computer Science 1
Formatted Input, Output & File Input, Output
Reading from and Writing to Files
Using string type variables
Strings Skill Area 313 Part C
Reading from and Writing to Files
Presentation transcript:

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 not return. 2.Write a function call for swap_values. Use the values num1, num2 as arguments. 3.Write a function header for the function swap_values that takes three integers, num1, num2 and does not return.

Strings #include Character sequence enclosed in double quotes

Declaring & Initializing Strings string objectName = value; –string str1 = “Good Morning”; –string str2 = str1; –string str3 = str1 + str2; string objectName(stringValue); –string str4(“Hot”); –string str5(str4 + “ Dog”); string objectName(str, n); –string str6(“Good Morning”); –string str7(str6, 5); //str7 = “Morning”;

Declaring & Initializing Strings string objectName(str, n, p); –string str8(“Good Morning”); –string str9(str8, 5, 2); //str9 = “Mo”; string objectName(n, char); –string str10(5, ‘*’); //str10 = “*****”; string objectName; –string message; //message = “”;

String Input string message; cin >> message; cout << message; This may have problems….

Extraction Operator >> >> skips any leading whitespace characters >> stops at (before) the first trailing whitespace character trailing whitespace character is left in stream, and will become “next” leading whitespace character.

String Input Using >> string firstName; string lastName; cin >> firstName >> lastName; Input stream: Joe Hernandez 23 Results: “Joe”“Hernandez” firstNamelastName

getline() Function >> cannot be used to input a string with blanks in it. Use getline(inFileStream, str) First argument is an input stream variable (cin), second is the string variable. string message; getline(cin, message);

getline(inFileStream, str) getline does not skip leading whitespace characters getline reads all characters (including blanks) into the string. getline stops when it reaches ‘\n’ ‘\n’ is not stored in the string variable

String Input: getline string firstName; string lastName; getline (cin, firstName); getline (cin, lastName); Input stream: Joe Hernandez 23 Results: “Joe Hernandez 23”? firstNamelastName

String Operations

Text File Input Text file is an ASCII file Handles: –Integers –Floating point numbers –Characters

Text File Input #include Declare a local filename, of type fstream: Examples: fstream fileName; fstream inFile;

Commands fileName.open(“actualName.dat”, ios::in); fileName.eof() fileName >> var; var = filename.get(); filename.close(); Makes the connectionTells it’s for input // Returns true when it reaches the end of file // Just like cin, except from the file // (>> skips white spaces for char input) // for char input, includes white spaces // Closes an opened file

Example: Integer file #include using namespace std; int main() { int a; fstream inFile; inFile.open(“int.dat”, ios::in); cout << “The values in the file were\n”; while (!inFile.eof()) { inFile >> a; cout << a << “ “; } cout << “\nend of file\n”; inFile.close(); } int.dat Output: The values in the file were end of file

Example: String as Filename #include using namespace std; int main() { int a; string fileName = “int.dat”; fstream inFile; inFile.open(inFile.c_str(), ios::in); … inFile.close(); }

Mixed Data Input must match the file layout Example file: 3124 Hammer HandSaw Wrenches 7.63 integer string float 2 spaces 1 space

int main() { int partNum; string part; float price; string file = "parts.dat"; fstream inFile; inFile.open(file.c_str(), ios::in); cout << "\nPart Number" << setw(12) << "Price" << setw(18) << "Description\n\n"; inFile >> partNum >> part >> price; while (!inFile.eof()) { cout << setw(8) << partNum << setw(8) << '$' << setw(8) << price << setw(5) << " " << part << endl; inFile >> partNum >> part >> price; } inFile.close(); return 0; }

File existence fail() string file; cout << "Please enter a file name: "; getline(cin, file); fstream inFile; inFile.open(file.c_str(), ios::in); if (inFile.fail()) { cout << "Sorry, I can't find the file\n"; exit(1); }

Example code: Located in: /class- files/samples/strings/ stringpractice.cpp stringinput.cpp stringoperations.cpp Located in: /class-files/samples/files ask.cpp infile.cpp mixed.cpp int.dat parts.dat