Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

djsomers

simple file problem in C++

This topic is 6112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I''m sure that this question is so simple, I''m going to kick myself if someone answers it. Anyway, I''m trying to manipulate a file by asking the user to enter some info, based on their name, if their already exists in the file, the program should go to that record position and overwrite it with hte new record. But the problem with my code is that everytime I try it, the new record is appended at the end and the old record remains. I''m using C++ to get the filepos (fp.tellg() of the record to overwrite, then using this to go to this postion in the file and writing the new record, but it don''t wurk !!! Here''s the source:
  
#include <conio.h>
#include <iostream.h>
#include <fstream.h>

const char *filename="testfile.txt\0";
const char *fileformat="testfileformat\0";

char username[256]="\0";
char filefmt[15]="\0";
const char *temusername="testname2\0";

int filepos;
bool overwrite=false;

struct recordout
{
   char username[256];
   short dummy;
}recout;

struct recordin
{
   char username[256];
   short dummy;
}recin;

bool createfile()
{
   cout <<"\nPlease enter your name: ";
   cin >> username;

   // place user values in record

   strcpy(recout.username,username);
   recout.dummy=45;

   fstream fp;
   char userin;

   // check if file exists by opening for reading

   fp.open(filename,ios::in|ios::binary);

   if(!fp)
   {
      // create file for writing

      fp.open(filename,ios::out|ios::binary);

      if(!fp)
         return false;

      fp.write(fileformat,strlen(fileformat)+1);
   }
   else
   {
      // check file format

      fp.read(filefmt, sizeof filefmt);
      if(strcmp(filefmt,fileformat)!=0)
         return false;

      // search for corresponding username in file

      while(fp.read((char*)&recin,sizeof recin))
      {
         if(strcmp(recin.username,username)==0)
         {
            do
            {
               cout << "\n\nDo you wish to overwrite previous entry: ";
               cin >> userin;
            }while(userin !=''y'' && userin !=''n'');

            // overwrite

            if(userin==''y'')
            {
               // get the current location in the file

               filepos=fp.tellg();

               // go to the location in the file minus the rec structure

               fp.seekg(filepos-258,ios::beg);

               // store current location of file pointer

               filepos=fp.tellg();
               fp.close();
               overwrite=true;   // user wishes to overwrite record

            }
            else
            {
               fp.close();
               return true;
            }
         }
      }
   }


   if(overwrite)
   {
      // open file for writing

      fp.open(filename,ios::app|ios::binary);

      if(!fp)
         return false;

      fp.seekg(filepos,ios::beg);  // go to location of username (record to overwrite)

      fp.write((char*)&recout,sizeof recout);
      fp.close();
      return true;
   }
   else
   {
      // close open file

      fp.close();

      // file already exists, enter new user data

      fp.open(filename,ios::app|ios::binary);

      if(!fp)
         return  false;

      // clear eof flag

      if(fp.eof())
         fp.clear();

      // goto end of file

      fp.seekg(0,ios::end);

      fp.write((char*)&recout,sizeof recout);
      fp.close();
   }

   return true;
}


void main()
{
   if(createfile())
      cout << "\n\nOK";
   else
      cout << "\n\nError";

   getch();
}
  
any help please. djsomers

Share this post


Link to post
Share on other sites
Advertisement
Hehe, sounds close enough to a school-assignment...
My suggestion is that you read all the entries into a vector and change them in memory, and when the user quits (or want to save) you write all the entries back to the file.

Good luck with the assignment

Share this post


Link to post
Share on other sites
Try using seekp() (which is the basic_ostream function, while seekg() is basic_istream). The implication of using seekg() is that you''re manipulating the wrong pointer (the extraction pointer, rather than the insertion pointer).

Disclaimer: Haven''t tried out any code, but I read the docs.

Share this post


Link to post
Share on other sites

1) on the line that says:
}
else
{ // check file format
fp.read(filefmt,sizeof(filefmt, sizeof filefmt));
if(strcmp(filefmt,fileformat)!=0)
return false;

change fp.read(....) to

fp.getline(filefmt,sizeof(filefmt));

this is because before it was reading the first 15 chars and was getting a freaking space in there...so the comparision was never right. Now it's reading the first 15 lines or until it hits the \n (by default)

This right there fixed everything, and it return's true now....
geez

oh btw, if you had just run the debugger and stepped through the function, and watched the variables, you would have seen this...


Pactuul


Edited by - Pactuul on November 22, 2001 4:50:33 AM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!