eof() ?
Mar 6, 2010 at 11:28pm UTC
I can't really figure out why I need to use eof() with files... I keep reading on it in my book for college but its just not doing it for me any help would be appreciated.
Mar 6, 2010 at 11:43pm UTC
eof lets you know if you've reached the end of the file.
Without it, how would you know when to stop reading data from the file?
Or what if the file was shorter than you expected?
Mar 7, 2010 at 12:15am UTC
But where would I use it in this program that I made?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
#include <iostream>
#include <fstream>
using namespace std;
int main( ) {
char c;
int userPick;
int userFilePick;
ifstream outFile;
cout << "1: Lamb of god" << endl;
cout << "2: Six feet under" << endl;
cout << "3: Pantera" << endl;
cout << "4: Slayer" << endl;
cout << "> " ;
cin >> userPick;
cout << endl;
switch ( userPick )
{
case 1:
cout << "1: Ashes Of The Wake" << endl;
cout << "> " ;
cin >> userFilePick;
if ( userFilePick == 1 )
{
outFile.open( "AshesOfTheWake.txt" );
while ( outFile.good( ) )
{
c = outFile.get( );
if ( outFile.good( ) )
cout << c;
}
}
break ;
case 2:
cout << "1: the day the dead walks " << endl;
cout << "> " ;
cin >> userFilePick;
if ( userFilePick == 1 )
{
outFile.open( "TheDayTheDeadWalks.txt" );
while ( outFile.good( ) )
{
c = outFile.get( );
if ( outFile.good( ) )
cout << c;
}
}
break ;
case 3:
cout << "1: cowboys from hell " << endl;
cout << "> " ;
cin >> userFilePick;
if ( userFilePick == 1 )
{
outFile.open( "CowboysFromHell.txt" );
while ( outFile.good( ) )
{
c = outFile.get( );
if ( outFile.good( ) )
cout << c;
}
}
case 4:
cout << "1: Disciple " << endl;
cout << "> " ;
cin >> userFilePick;
if ( userFilePick == 1 )
{
outFile.open( "Disciple.txt" );
while ( outFile.good( ) )
{
c = outFile.get( );
if ( outFile.good( ) )
cout << c;
}
}
default :
cout << "Not an option: \n" ;
break ;
}
outFile.close( );
return 0;
}
Mar 7, 2010 at 12:34am UTC
You don't really need to because you're already using good()
which checks for EOF as well as other things.
Mar 7, 2010 at 12:36am UTC
So is good better to use then EOF?
Mar 7, 2010 at 12:40am UTC
Always loop using good (). Once the file is no longer good (), then you should check to see whether it failed due to eof () or something else. This avoids infinite loops.
BTW, why did you name an input stream "outFile"?
Mar 7, 2010 at 1:14am UTC
A mistake, and thanks for the help
Topic archived. No new replies allowed.