COSC.497.001, Spring 2005
C++ For Game Developers
Yoram Chisik  

Assignment 4
Write to File Instructions


--- Class Links ---
Course Syllabus


How to Write to A File

Writing to a file is a very simple operation is you need to do is specify the file to write to and then write to it in a similar manner that you have used cout to write to the screen so far.



In the example above we create a new file called out.txt which we store at the root leve lf the C drive and write the expression "Hello world" to it.

We begin by including the fstream library to our program for file input/output capabilities

We then use the ofstream command to create a new file giving it the name myFile and specifying a directory path

After we create the file we check to see the file was successfuly created, displaying an error message to the screen if not

You write to the file in the same way you would use cout replacing cout with the name of the file.

When you have finished writing to the file, close the file using the close method.

Updated 4/02/05