Welcome to Dream.In.Code
Become a C++ Expert!

Join 136,941 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,868 people online right now. Registration is fast and FREE... Join Now!




Problem with seekg

 
Reply to this topicStart new topic

Problem with seekg

Steffan
31 Mar, 2008 - 04:23 PM
Post #1

New D.I.C Head
*

Joined: 14 Jan, 2008
Posts: 29

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 );

}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 11:00PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month