200 likes | 349 Views
Exam #2 Some notable string functions File Input/Output Reading: 15.1 - 15.5. Agenda. String Functions. There are some string functions I would like you to become familiar with. You will need these for HW #9. Function Definitions: int find(char c, int pos); int length();
E N D
Exam #2 Some notable string functions File Input/Output Reading: 15.1 - 15.5 Agenda
String Functions There are some string functions I would like you to become familiar with. You will need these for HW #9. Function Definitions: int find(char c, int pos); int length(); int atoi(char * str); //char * str means a C string string substr(int pos, int len);
String Function - Find The find function will return the position of a character in a string. The first parameter is a character to look for. The 2nd parameter, which is optional, is the position in the string you want to start looking at. string hi = “Hello, how are you?”; int spacePos1 = hi.find(' '); int spacePos2 = hi.find(' ', 7); //Or, without hardcoding any positions: int spacePos2 = hi.find(' ', spacePos1 + 1);
String Functions - Length You should already be familiar with length(). It tells you how many characters are in a string. string hi = “Hello World”; int len = hi.length();
String Functions substr() is pretty self explanatory – it pulls a substring from a string. The first parameter is the start position. The 2nd parameter is the length of the substring. string hi = “Hello World”; string hello = hi.substr(0,5); string world = hi.substr(6,5); string firstWord = hi.substr(0, hi.find(' '));
String Functions - atoi atoi() lets you convert a string to a number. For example, if you read a string from user input that has numbers and spaces, you might want to parse out one of the numbers and use it in an equation. string s = “There are 52 apples”; string numApplesS = s.substr(10,2); int numApples = atoi(numApplesS.c_str());
File Input/Output We have gotten input from the user and printed output to the screen. What if we want to get input from a file, or print results to a file? File I/O is useful for large data sets. For input, your user doesn't want to type in lots of data. For output, sometimes your user might appreciate having result data in a searchable file.
Streams A stream represents the flow of data to/from a source. Keyboard – cin is an object of type istream, which is in the iostream header Screen – cout is an object of type ostream, which is in the iostream header Read File – we create an ifstream object. The ifstream class is in the fstream header. Write File – we create an ofstream object. The ofstream class is in the fstream header.
ifstream To create an object of type ifstream, which will let us read from a file, we type the following: ifstream inFile (“filename”); The ifstream class supports the << and >> operators on objects in the same way that we use them with the objects cin and cout. *Include the fstream header
ifstream Read a single integer from a file (note: there could be a mistake where the first line is not an integer): ifstream inFile (“filename”); int x; inFile >> x; Read the first line from a file: ifstream inFile (“filename”); string line; getline(inFile, line);
ifstream – possible error Some of the fstream functions, particularly the constructor, require a native C string as a parameter. Remember, native C strings are different from C++ strings (see Lesson8.ppt). So what happens if you forget? You might get this error message: readFile.cpp:12: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char>>::basic_ifstream(std::string&)' How do you make sense of this error?
ifstream – possible error Decoding a type error message Clue #1: “no matching function”. This means that the fstream constructor has one definition, and you are trying to use it in a way that doesn't match that definition. Clue #2:The message shows you what data type it expects and what data type you are sending, even if it is shown in a more complicated way than you are used to. (That's the red text) Fixing it: You can convert a C++ string to a native C string using a function in the C++ string class! myString.c_str();
ifstream – check status ifstream has the following statuses: good, eof, fail, bad Use these statuses to check if reading from the file was successful (similar to how we check cin.good()) You can check if a file opened successfully, if data was read successfully, and check when the end of file (eof) has been reached. Example: readFile.cpp, countA.cpp
Write to a file - ofstream Writing to a file is very similar to reading from a file. We use the ofstream class instead of the ifstream class. ofstream outFile (“outFile.txt”); if (outFile.good() == false) { cout << “Error opening file: outFile.txt”; } else { outFile << “some text”; } Example: writeFile.cpp
Write to a file - ofstream WARNING: Be careful what file you write to! You could accidentally overwrite something you want.
File I/O – Now what? How is this useful? We will use the example from your book. We have a file with distances between two cities (assume these are accurate): We can use this file as if it were a database.
File I/O – City Distances Example We can parse out the two cities and the miles on each line. To do that, we have to be able to find the separating characters. That is, double quotes (“), spaces ( ), and commas (,). To find the first double quote, we type: int firstQuotePos = line.find('\”'); Because double quotes are a special character in C++ (they have meaning to the compiler), we must escape them with a backslash. What about finding the second one? Well, that's more complicated.
File I/O – City Distances Example We can parse out the two cities and the miles on each line. To do that, we have to be able to find the separating characters. That is, double quotes (“), spaces ( ), and commas (,). To find the first double quote, we type: int firstQuotePos = line.find('\”'); Because double quotes are a special character in C++ (they have meaning to the compiler), we must escape them with a backslash. What about finding the second one? Well, that's more complicated.
File I/O – City Distances Example Let's write our own function to get the x occurrence of a character. And let's write our own functions to get each “field” we need: int getIndexOfChar(string line, char c, int occ); string getFirstCity(string line); string getSecondCity(string line); int getMiles(string line); Example: cityDistances.cpp
Homework #9 Homework is posted online