Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Lab 7 Part 2

File I/O
It often happens in real-life problems that one needs to handle large amounts of data. For example, in your last lab you implemented a phone-list using a linked list. However, your phone list only had the contact details of only a few people; those that you added after running the program. Your implementation of the phone list was of no use at all because as soon as the program terminated, all data that you had entered would be lost. Now if only you could save that data each time you exited the program and load it back each time you ran it again then you would have something really useful you very own phonebook software! The question now is how would you do it? And that is where file input/output comes in. What is File I/O? C++ has an inbuilt library with defined functions that lets us manipulate files and the data they contain. As always, to use any library we must include it at the top. The syntax to include this particular library would be as follows: #include <fstream> Putting this at the top of your code will let you take input from a file or output data to it as well allow you access to a lot of other useful functions as we will soon see. In other words it will allow you to access the file stream. You will see that this is not very dissimilar from the <iostream> (input-output stream) that you are so familiar with. The first thing you need to do is to create a stream variable. A stream variable allows you to connect to a file. The syntax for that is: ifstream fin; or ofstream fout;

fin and fout are just random variable names. You can place anything here. The if in ifstream stands for input file and of in ofstream stands for output file. Now that you have created the variables you need to connect them to a file. Otherwise they are quite useless. More syntax: fin.open (myInputFileName.fileType); This will connect your fin variable to the file that you have specified. For example, the file you will be using for input today is PhoneBook.csv

Note that it is absolutely essential to specify the file type. The file that you wish to connect to must also be present in the same directory as your C++ code. Now that you have the basics down, its time to get to the task at hand:

Task 1: Creating a phonebook from provided data.


You have been provided with an Excel Comma Delimited File (.csv format) named PhoneBook. If you open the file you can see that the first column contains names and the second column contains the phone numbers. You need to extract this data line by line and use it to create nodes. To do that you need to know a couple of more things that we have outlined below. 1. csv file A csv excel file is comma delimited. This means that it separates data into separate columns whenever it encounters a comma. In other words, there is a comma between each element of a single row in the file. But instead of that comma all you see is the column boundary, but trust us when we say that its there. P.S. What do you think is between the last element in a row and the first element of the next row? It is not a comma! 2. getline ( istream& is, string& str, char delim ); 3. getline ( istream& is, string& str ); These are the two functions that you are going to be using to extract data from the file. The first argument you need to provide is the input stream, which in our case was the variable fin; the second variable is the string variable that you want the input to be stored in and the third argument is the delimiter or in other words the character up to which you want the input to be taken. So as a short example, writing: getline(fin, myString, ,); would read the file up till the first comma encountered and store the entire string preceding that point into our string variable myString. Now the second version of getline works in much the same way as the first one. You may have noticed that the only difference is that you are not specifying the delimiter in the second version of the function and in that case the default delimiter \n that is the end-line character is used. 4. end of file character Since you started taking this course one thing that has happened is that you have gotten to know of weird characters that you didnt know existed such as the null character or the end-line character. The next character we are going to introduce you with is the end-of-file character. This is a special invisible character at the end of each file which acts as a marker to signify that the end of file has been reached. This would tell you when to stop when reading the file, e.g.

while ( !fin.eof() ) { getline(fin, a); cout<<a<<endl; } Translating it to English this means: read each line from the file connected to fin; store it in the string variable a; then output the string to the screen and keep repeating this until the file ends.

Now you have all the tools that you need to start doing your task. Download the file phonelist.c from LMS and put it onto your server. This file contains the code that you were asked to implement in the last lab. Two functions have been added namely loadPhoneBook() and storePhoneBook(). Your job is to write the code for these functions. i) void loadPhoneBook()

The stream variable fin has been declared for you and it has been connected with the input file. Your task is to use the getline() functions to extract data from file and use it to create nodes. You may call the function void add_node_at_end (string nom, int number); to create the nodes. It has been defined such that it creates a phonebook entry with the name and phone number stored the variables nom and number respectively. ii) void storePhoneBook() The stream variable fout has been declared for you and has been connected with an output file. You need to traverse your list and store each name and number sequentially in the same format as was found in the input file provided by us. To do this you need to know about another command. Just as we have cout<< in iostream we have a fout<< in ofstream. Typing fout << This is such a cool lab. will write this sentence into your output file.

Task 2
In this task you have been provided with a large text file. Your task is simple: Implement a function void findSequence (char* mystring, int n). Your function should parse the text file character by character and output the number of times the string mystring was encountered in the text file. Also output the line number in which the string was found each time. The first line should be considered line number 1. You can assume that no line will be longer than 100 characters.

For character by character parsing you might find the following two functions useful: fin.get ( char& c ); Extracts a character from the stream and stores it in c. fin.get (char* s, streamsize n ); Extracts characters from the stream and stores them as a c-string into the array beginning at s. Characters are extracted until either (n - 1) characters have been extracted or the delimiting character '\n' is found. The extraction also stops if the end of file is reached in the input sequence or if an error occurs during the input operation. If the delimiting character is found, it is not extracted from the input sequence and remains as the next character to be extracted. The ending null character that signals the end of a c-string is automatically appended at the end of the content stored in s.

You might also like