Left Quote    Copy from one, it's plagiarism; copy from two, it's research.
- Wilson Mizner    
Right Quote
[login] | [Register]

Beginning Visual C++ 6.0

by: bs0d
Page: 9 of 10
(View All)

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:

Thats it!

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:



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.

1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10  |  
Next »

No Comments for this page.

You Must be logged in or a member to comment.

Tutorial Stats

Tutorial Stats

1 Total Comments
4 Rating of 5 (2 Votes)


Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article



· C Tutorial
Your Article Here

"" Copyright © 2002-2019; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank