String Constructors string str; // creates an empty //string string str(“abc”); // creates a string // from a C-string string str(aString); // creates.

Slides:



Advertisements
Similar presentations
Getting Data into Your Program
Advertisements

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.
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.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
The Standard String Class Is actually a template: –typedef basic_string string This means you can have strings of things other than chars.
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.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
TEXT FILES. Text Files Files Data saved in external storage and can be referenced by a single name File types Document, image, audio, video, etc. Program.
File I/O ifstreams and ofstreams Sections 11.1 &
1 Streams In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
1 CS161 Introduction to Computer Science Topic #13.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
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.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
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,
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.
Vectors One-Dimensional Containers. Problem A file contains a sequence of names and scores: Ann92 Bob84 Chris89... Using OCD, design and implement a program.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
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.
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.
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.
ifstreams and ofstreams
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
Basic Input and Output Operations
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by
Quiz Next Monday.
File I/O.
File I/O.
More About File Reading
Basic File I/O and Stream Objects
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
File I/O.
CS150 Introduction to Computer Science 1
File I/O.
CHAPTER 4 File Processing.
C++ Programming: chapter 6 – iostream
Reading from and Writing to Files
Programming with Data Files
Reading Data From and Writing Data To Text Files
(Dreaded) Quiz 2 Next Monday.
ifstreams and ofstreams
Programming Strings.
File I/O in C++ II.
Lecture 3 More on Flow Control, More on Functions,
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

String Constructors string str; // creates an empty //string string str(“abc”); // creates a string // from a C-string string str(aString); // creates a string by // copying

String Methods str.substr(pos,len); // returns a substring starting a pos of // length len. str.length(); // returns the length os str. str.insert(pos,str2); // inserts str2 into str starting a pos. str.find(str1,pos); // finds str1 in str starting pos and returns // position, or npos if not found str.find(str1); // finds str1 in str and returns position, // npos is not found.

String Comparsions, Concat. str1 == str2 str1 != str2 str1 < str2 str1 <= str2... str1 + str2

Example - Sorting void swap(string& x, string& y) { string t = x; x = y; y = t; } int main(void) { string a[] = { "that", "cat", "rat", "bat", "sat"}; int size = 5; for(int j = 0; j < size; j++) for(int i = 0; i < size j; i++) if(a[i] > a[i+1]) swap(a[i], a[i+1]); for(int i = 0; i < size; i++) cout << a[i] << " "; cout << endl; }

Example – Text Processing Remember the rule: 'i' before 'e' except after 'c'? Suppose we want to write a program that tests if a word follows the rule. For example, “grief” and “receipt” are fine, but “caffeine” is not.

Solution #include using namespace std; int main(void) { string word; cout “; cin >> word; if(...) cout << “good” << endl;' else cout << “bad” << endl; }

Streams A stream is a flow of characters (or other data). If the flow is into the program, it is an input stream. If the flow is out of the program, it is an output stream. cin and cou t are two examples of streams. streams can also be related to files and can be used to read and write files. A third kind of stream is a pipe, where the input or output comes from or goes to another program.

Reading from a File The functions which deal with files are in the fstream library (for file stream). To read from a file, you must open the file for reading and associate an input stream with it. Reading from an input stream uses the extraction operator, >>, just like reading from cin.

Reading - Example #include using namespace std; int main(void) { ifstream myIStream; myIStream.open(“MyFile.txt”); string word; myIStream >> word;... myIStream.close(); } This opens the file “MyFile.txt” and associates the stream myIStream with the file. One string (word) is read in from the file.

Files have Two Names Note that there are two names associated with a file; the external name, associated with the operating system – in this case, “MyFile.txt” and and internal name for the stream, MyIStream. Once the file is opened, the program uses just the name of the stream.

Closing a File Closing a file disconnects the stream from the file, and performs some clean-up operations, so don't forget to close a file once you're done with it.

Writing to a File The functions which deal with files are in the fstream library (for file stream). To write to a file, you must open the file for writing and associate an output stream with it. Writing to an output stream uses the insertion operator, <<, just like writing to cout.

Writing - Example #include using namespace std; int main(void) { ofstream myOStream; myOStream.open(“MyOutput.txt”); string word = “foobear”; myOStream << word;... myOStream.close(); } This opens the file “MyOutput.txt” and associates the stream myOStream with the file. One string (word) is written to the file.

Appending to a File You can append text to a file, by using the code: ofstream myOStream; myOStream.open(“MyOutput.txt”, ios::app); Don't forget to close the file when done writing to it.

Checking for the end of file The method eof() is a Boolean method that will check if the program is at the end of the file, e.g., myIStream.eof(). Beware – it only returns true once you've tried to read past the end of the file (and not when you're about to read past the end of the file). Using (myIStream >> word) as your test is probably a better practice.