I am writing a program and i need to reset my file to the begging. I used seekg on a visual C++ 2008 compiler and it worked but when i tried to do it on a visual studio 2005 compiler my file does not reset to the begging and the program only worked once. here is my code.
CODE
#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;
const int MAX_CHAR = 30;
void ReadNamesAndScoresToFile(ofstream & scores)
{
scores << "Joe Jones" << endl << 85 << endl;;
scores << "Bill Smith" << endl << 76 << endl;
scores << "Sue Adams" << endl << 99 << endl;
scores << "John Williams" << endl << 65 << endl;
scores << "Mary Thompson" << endl << 88 << endl;
scores << "Joan Davis" << endl << 70 << endl;
scores << "Margaret Eads" << endl << 92<<endl;
scores << "Tom Walters" << endl << 78 << endl;
scores << "Deb Baker" << endl << 72 << endl;
scores << "Ben Welch" << endl << 75 << endl;
}
void main()
{
char name[MAX_CHAR], temp[MAX_CHAR];
int test_score = 0, flag;
ofstream scores;
ifstream infile;
scores.open("scores.dat", ios::app);
ReadNamesAndScoresToFile(scores);
do
{
cout<<"Name to find? ";
cin.getline(name, MAX_CHAR);
infile.open("scores.dat");
flag = 0;
while(!infile.eof())
{
infile.clear();
infile.getline(temp, MAX_CHAR);
infile>>test_score;
infile.get();
if(strcmp(name, temp)==0)
{ cout<<"\nThe score for "<<name<<" is "<<test_score<<endl<<endl;
flag = 1;
}
}
if(flag==0 && strcmp(name,"quit") != 0)
cout<<"\nName not found\n\n";
infile.close();
} while(strcmp(name,"quit") != 0 );
}