QUOTE(Lizardking @ 22 Apr, 2008 - 04:08 PM)

ugh...input and output files are annoying if you dont get them. Anyways, Can someone explain to me how ifstream and ofstream work. And also do you have to make a file first before making the code? or does the compiler does it by itself?
I have a basic example of ifstream and ofstream below with comments to help example what is being done hope this helps explain how to use fstream a little better.
CODE
//Demonstrating the use of input/output file
//Normally the program will execute but will not display anything.
//U can make the program display something when its finished using
//cout << "Program is done file read and outputted to file" << endl;
//something to that nature
//***********************************************
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream> //this header allows me to use files in the program
using namespace std;
int main ()
{
//helps the program understand wat is being read in the file
string firstInitial;
string lastName;
float socialNum;
ifstream inData;
//this is the prototype needed to start the read of the input file
ofstream outData;
//this is the prototype needed to have ur program output ur data into an output file
//this actually creates a new file.
inData.open ( "example.txt" );
//the inData is being defined and being opened by the program.
//note the name in quotes in the ( ), that is the file i made in order to work in the program.
//the file must be saved inside the same folder that u saved ur code for the program.
outData.open ( "newExample.txt" );
//the outData file is made with this instruction and u must name it differently than the inData file.
//the program will save this file with ur source code and inData files.
inData >> firstInitial >> lastName >> socialNum;
//this tells the program what is being read in the file.
//the names come from wat i defined at the beginning of int main.
cout << fixed << showpoint; //this is to read the number as how its read and not by sci fig notation. this is done with the #include <iomanip> header
outData << socialNum << " " << lastName << " " << firstInitial << endl;
//for the outData u can manipulate how the program outputs the data being read.
inData.close ();
outData.close ();
//always remember to close both inData and outData files
return 0;
}