Files

If you've ever spent anytime at all looking at the programs in your computer, you may notice that very few are one file. Programs are all built on eachother, much like the programs you've been making are all possible due to the header files we've been including. In the last tutorial we took in a name and grade and then outputted it to the screen. We are going to use the same idea, and then output it to a file instead. To do this we need to include the header fstream.

Once the header is included we can continue setting the variable outfile to type ofstream. This is a special variable and we don't assign a value to it using an equal sign, instead we first need to specify which file to open. Because we are going to be writing to this file, if it isn't there, it will try and create one, if it is there, it will erase the file. To set the file to the variable we do the name followed by a period (.) followed by open then open parenthases then the file you want to write to in quotation marks and close parenthases. Writing to the file is the same way we write to the screen, using two less than (<) signs. Before the program is finished we need to close the file. To do this we do the variable name followed by a period then close then open and close parenthases. To show you how to do this in the previous program, I have made the new code red.

#include <iostream>
#include <string>
#include <fstream> //Include the file stream header
using namespace std;
void main()
{
ofstream outfile; //Declare the variable called outfile
outfile.open("outdata.txt"); //Open the file for writing

string name;
char grade;
do
{
cout<<"\nEnter the student's last name (quit to end) ";
cin>>name;
if (name == "quit")
{
break;
}
cout<<"\nEnter the student's grade ";
cin>>grade;
switch(grade)
{
case 'a':
case 'A':
case 'b':
case 'B':
case 'c':
case 'C':
case 'd':
case 'D':
case 'f':
case 'F':
outfile<<name<<" "<<grade<<endl; //Exactly what will be written in the file
break;
default: cout<<"Error";
}
}while(name!= "quit");
outfile.close(); //close the file
system("PAUSE");
}


Now that we've written a file, let's say you wanted to call that file from another program. The process of calling a file to write from is almost the same as for writing to a file. Again we include the header file, and for the variable type, it is ifstream. We set the file to be read using the variable name followed by a period (.), followed by open then parenthases and the file to open in quotes, like when you wrote to the file. To display the file, we simply set the infile to the appropriate variables that are in the file (string, char, etc) and then use the variable name and two greater than signs (>) and variables. We then use the cout command to display them on the screen. At the end we close the infile the same way we closed the outfile. Because we don't know how long the file is, we use the variable name then a period(.) then eof to tell the compiler which file you it should keep checking. The eof stand for end of file, which is will cause it to stop looping. Assuming you have a file called outdata.txt in the local file, this should work. I put the new code that needs to appear in red, because it is confusing.

#include <iostream>
#include <string>
#include <fstream> //Include the file header
using namespace std;

void main()
{
ifstream infile; //Declare the variable called infile
infile.open("outdata.txt"); //Open the file we wrote before
string name;
char grade;
do
{
infile>>name>>grade; //Set the variable to the contents of the file
cout<<name<<" "<<grade<<endl;
}
while(!infile.eof); //Keep looping until reaching the end of the file
infile.close(); //Close the file

system("PAUSE");
}


If you follow the examples here you shouldn't have any trouble writing to and reading from files. You could also put a path like c:\outfile in the quotation marks, which would specify where on the drive the file is stored. Besides that though, there isn't much more to say about writing to files.

Next Section: Arrays
Twitter








Link Directory | link directory | GeekySpeaky: Submit Your Site! | Blog Matter

Copyright © 2004-2017 Sean Noble, All rights reserved
Read our Disclosure Policy