Writing to the file
You write to the file the same as you would output data to the screen. If your file was defined as yourFile, then you would do something like this : yourFile << "Hello World" << endl; --Thats it. But what about when you want to add more data? How will you differentiate between what was written the first time, to what was written the second time? To do this, you can add a character to the end. So instead of: yourFile << "Hello World" << endl; - You can do: yourFile << "Hello World" << "#" << endl; Add # to the end of each write. This will give you a point to go by in the future when you want to get and change data from the file.
The future is now! first we open the file (like we did earlier), then you can check if it did open (why not). We can now use the eof function. This stands for End of File. So, we can use a while loop and the eof function to access data from the file until the file has reached its end. Heres the syntax: yourfile.eof() - This function also returns a boolean value. Look below how we use it in our program:
We opened the file, checked that it did open. used getline() to get info from the file. The while loop will display var while it is not the end of the file. Once complete, we close the file. And if the file did not open, we display the error message.
We are not yet done with writing the file and accessing it. If you wrote to the file as I indicated earlier, and you displayed the record to the screen it would read: Hello World#. We want to ignore the # at the end. So, there is the ignore function. This function will ignore, consume or pass by a character you specify. Theres two arguments to the ignore function. Heres the syntax: cin.ignore(num, delimChar); --You can use it with cin or your file object name (we've been using yourFile). So if we said: yourFile.ignore(555, '#'); The ignore function stops reading and discarding characters once its consumed the num (555) of characters or has reached the delimChar (#). So obviously, it would stop at the # in our case, we dont even have 555 characters.
Closing Your Open File
To close your file that you have open, you will use the close function. The syntax looks like this: nameyoumade.close(); . Theres no need to specify the file name inside because the computer already knows the file open when you gave it its object name (nameyoumade).Following tradition, heres a bit of code showing you how to close the file after you're done writing data:
You can test this on your own and work with it a bit. You can format the output to the file just like you formatted the output to appear to the screen. Now, we've covered everything. So lets go ahead and have a complete code sample that will go through every process using what we learned about working with files:
WRITE TO FILE:
READ FROM FILE:
On the reading from the file example we used: getline(yourFile, name, '#'); -- This reads all the characters in the first record, up to and including the #. And the yourFile.ignore(1); at the end will ignore the newline character 'n', which is the same as the enter key. when endl; is stated we're taken to the next line, well internally this new line is considered a character and we must ignore that or it will skew our results.
No Comments for this page.