// fileio.cpp CIS 205 C/C++ 04/17/2002
// this program performs file disk input and output
#include <iostream.h>
#include <fstream.h>
#define FILENAME "C:\\temp\\myfile.txt"
void writeFile(char []);
void readFile(char []);
int main(void)
{
writeFile(FILENAME);
readFile(FILENAME);
return 0;
}
void writeFile(char fileName[])
{
fstream myIOFile;
myIOFile.open(fileName, ios::app);
if(!myIOFile)
{
cout << "Could not write to " << fileName << endl;
}
else
{
int i = 0;
char myPhrase[] = {"The best laid plans of mice and men\nwill go awry we can't say when!"};
do
{
myIOFile << myPhrase[i];
i++;
} while(myPhrase[i] != '\0');
myIOFile << endl;
}
myIOFile.close();
}
void readFile(char fileName[])
{
char chrFileCharacter;
fstream myIOFile;
myIOFile.open(fileName, ios::in);
if(!myIOFile)
{
cout << "Could not read " << fileName << endl;
}
else
{
myIOFile.get(chrFileCharacter);
while(!myIOFile.eof())
{
cout << chrFileCharacter;
myIOFile.get(chrFileCharacter);
}
}
myIOFile.close();
}