Professional Documents
Culture Documents
Tuesday Lab 7
Tuesday Lab 7
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:
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.