Link to home
Start Free TrialLog in
Avatar of nfcdrummer
nfcdrummerFlag for Australia

asked on

I need help with file access

Well...

I have a huge problem which is slowing the production of my program RIGHT down.  It is a huge pain.

You cannot save or load a character properly.  Or should I say, at all.

Since I am self-taught,  I really don't know any good methods to do basically anything and am learning straight from the book.  I would really like someone to totally re-write my code for the functions I use, or tell me exactly what to do.  The most efficient way of this working, will get the points.

I'll post a few examples here:
void charstuff::WriteFileChar(char givenname[50]) {
   if ((PlayerFile = fopen(PlayFile, "rt")) == NULL) {
      PlayerFile = fopen(PlayFile, "wt");
      fclose(PlayerFile);
      PlayerFile = fopen(PlayFile, "rt");
      goto continuefile;
   }
//   CodeIt(PlayFile);
   while (!feof(PlayerFile)) {
      fscanf(PlayerFile,"%s\n",yourname.c_str());
      if (yourname != NULL) numchars++;
      if (yourname.c_str()
          == givenname) {         // These are used to see if the
         CharError -> ShowModal();// Character already exists. If it does,
         fileflag = 0;
         goto stopreading; }      // it returns an error window and stops
                                  // searching through the char file.

      for (tempint = 1 ; tempint < 180 ; tempint++)
         fscanf(PlayerFile,tempstring);
      checkint++;
      if (checkint==200) {
         CharError2 -> ShowModal();
         fileflag = 2;
         goto stopreading; }
   }
   continuefile:
   fclose(PlayerFile);
   PlayerFile = fopen(PlayFile, "at");
      fputs(yourname.c_str(),PlayerFile);
      fputs(yourpassword.c_str(),PlayerFile);
      fputs(IntToStr(yourstr).c_str(),PlayerFile);
      fputs(IntToStr(yourint).c_str(),PlayerFile);
stopreading:
   fclose(PlayerFile);
//   CodeIt(PlayFile);
}

void charstuff::ReadFileChar(char givenname[50]) {
//   CodeIt(PlayFile);
   PlayerFile = fopen(PlayFile, "rt");
   while (!feof(PlayerFile)) {
      fgets(yourname.c_str(),50,PlayerFile);
      if (yourname == givenname) goto readitall;   // These are used to see if the
   }

readitall:
   fgets(yourpassword.c_str(),50,PlayerFile);
   ReadStats();
//   CodeIt;
   fclose(PlayerFile);
}

void charstuff::ListChars(void) {
//   CodeIt(PlayFile);
   PlayerFile = fopen(PlayFile, "rt");
   CharCount = -1;
   LoadCharForm->CharListBox->Clear();
   while (!feof(PlayerFile)) {
      fgets(yourname.c_str(),50,PlayerFile);
      fgets(yourpassword.c_str(),50,PlayerFile);
      ReadStats();
      CharCount++;
      LoadCharForm->CharListBox->Items->Add(yourname);
      LoadCharForm->CharListBox->Items->Strings[CharCount]
      = yourname;
   }
   fclose(PlayerFile);
//   CodeIt;
//LoadCharForm->CharListBox->Items.Strings = ("");

}

void charstuff::ReadStats(void) {
      fgets(IntToStr(yourstr).c_str(),50,PlayerFile);
      fgets(IntToStr(yourint).c_str(),50,PlayerFile);
      fgets(IntToStr(yourwis).c_str(),50,PlayerFile);
      fgets(IntToStr(yourdex).c_str(),50,PlayerFile);
      fgets(IntToStr(yourcon).c_str(),50,PlayerFile);
      fgets(IntToStr(yourcha).c_str(),50,PlayerFile);
      fgets(IntToStr(yourrace).c_str(),50,PlayerFile);
      fgets(IntToStr(youralign).c_str(),50,PlayerFile);
      fgets(IntToStr(yourkills).c_str(),50,PlayerFile);
      fgets(IntToStr(yourcharm).c_str(),50,PlayerFile);
      fgets(IntToStr(yourlevel).c_str(),50,PlayerFile);
      fgets(IntToStr(youratt).c_str(),50,PlayerFile);
      fgets(IntToStr(yourdef).c_str(),50,PlayerFile);
      fgets(IntToStr(yourmagic).c_str(),50,PlayerFile);
      fgets(IntToStr(yourcash).c_str(),50,PlayerFile);
      fgets(IntToStr(yourguild).c_str(),50,PlayerFile);
      fgets(IntToStr(yoursize).c_str(),50,PlayerFile);
      fgets(IntToStr(yourhp).c_str(),50,PlayerFile);
      fgets(IntToStr(yourmhp).c_str(),50,PlayerFile);
      fgets(IntToStr(yoursp).c_str(),50,PlayerFile);
      fgets(IntToStr(yourmsp).c_str(),50,PlayerFile);
      fgets(IntToStr(yourexp).c_str(),50,PlayerFile);
      fgets(IntToStr(yournexp).c_str(),50,PlayerFile);
      fgets(IntToStr(stuexp).c_str(),50,PlayerFile);
      fgets(IntToStr(warexp).c_str(),50,PlayerFile);
      fgets(IntToStr(palexp).c_str(),50,PlayerFile);
      fgets(IntToStr(kniexp).c_str(),50,PlayerFile);
      fgets(IntToStr(sorexp).c_str(),50,PlayerFile);
      fgets(IntToStr(wizexp).c_str(),50,PlayerFile);
      fgets(IntToStr(magexp).c_str(),50,PlayerFile);
      fgets(IntToStr(thiexp).c_str(),50,PlayerFile);
      fgets(IntToStr(scaexp).c_str(),50,PlayerFile);
      fgets(IntToStr(heaexp).c_str(),50,PlayerFile);
      fgets(IntToStr(scoexp).c_str(),50,PlayerFile);
      fgets(IntToStr(seeexp).c_str(),50,PlayerFile);
      fgets(IntToStr(ninexp).c_str(),50,PlayerFile);
      fgets(IntToStr(vilexp).c_str(),50,PlayerFile);
      fgets(IntToStr(pyrexp).c_str(),50,PlayerFile);
      fgets(IntToStr(sturun).c_str(),50,PlayerFile);
      fgets(IntToStr(warrun).c_str(),50,PlayerFile);
      fgets(IntToStr(palrun).c_str(),50,PlayerFile);
      fgets(IntToStr(knirun).c_str(),50,PlayerFile);
      fgets(IntToStr(sorrun).c_str(),50,PlayerFile);
      fgets(IntToStr(wizrun).c_str(),50,PlayerFile);
      fgets(IntToStr(magrun).c_str(),50,PlayerFile);
      fgets(IntToStr(thirun).c_str(),50,PlayerFile);
      fgets(IntToStr(scarun).c_str(),50,PlayerFile);
      fgets(IntToStr(hearun).c_str(),50,PlayerFile);
      fgets(IntToStr(scorun).c_str(),50,PlayerFile);
      fgets(IntToStr(seerun).c_str(),50,PlayerFile);
      fgets(IntToStr(ninrun).c_str(),50,PlayerFile);
      fgets(IntToStr(vilrun).c_str(),50,PlayerFile);
      fgets(IntToStr(pyrrun).c_str(),50,PlayerFile);
      fgets(IntToStr(stulevel).c_str(),50,PlayerFile);
      fgets(IntToStr(warlevel).c_str(),50,PlayerFile);
      fgets(IntToStr(pallevel).c_str(),50,PlayerFile);
      fgets(IntToStr(knilevel).c_str(),50,PlayerFile);
      fgets(IntToStr(sorlevel).c_str(),50,PlayerFile);
      fgets(IntToStr(wizlevel).c_str(),50,PlayerFile);
      fgets(IntToStr(maglevel).c_str(),50,PlayerFile);
      fgets(IntToStr(thilevel).c_str(),50,PlayerFile);
      fgets(IntToStr(scalevel).c_str(),50,PlayerFile);
      fgets(IntToStr(healevel).c_str(),50,PlayerFile);
      fgets(IntToStr(scolevel).c_str(),50,PlayerFile);
      fgets(IntToStr(seelevel).c_str(),50,PlayerFile);
      fgets(IntToStr(ninlevel).c_str(),50,PlayerFile);
      fgets(IntToStr(villevel).c_str(),50,PlayerFile);
      fgets(IntToStr(pyrlevel).c_str(),50,PlayerFile);
      fgets(friend1,50,PlayerFile);
      fgets(friend2,50,PlayerFile);
      fgets(friend3,50,PlayerFile);
      fgets(IntToStr(Companion1).c_str(),50,PlayerFile);
      fgets(IntToStr(Companion2).c_str(),50,PlayerFile);
      fgets(IntToStr(Companion3).c_str(),50,PlayerFile);
      fgets(IntToStr(Companion4).c_str(),50,PlayerFile);
      // resistances
      fgets(IntToStr(firres).c_str(),50,PlayerFile);
      fgets(IntToStr(colres).c_str(),50,PlayerFile);
      fgets(IntToStr(drares).c_str(),50,PlayerFile);
      fgets(IntToStr(poires).c_str(),50,PlayerFile);
      fgets(IntToStr(disres).c_str(),50,PlayerFile);
      fgets(IntToStr(parres).c_str(),50,PlayerFile);
      fgets(IntToStr(magres).c_str(),50,PlayerFile);
      fgets(IntToStr(minres).c_str(),50,PlayerFile);
      fgets(IntToStr(acires).c_str(),50,PlayerFile);
      fgets(IntToStr(stores).c_str(),50,PlayerFile);
      // put in items
      fgets(IntToStr(itemslot1).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot2).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot3).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot4).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot5).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot6).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot7).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot8).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot9).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot10).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot11).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot12).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot13).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot14).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot15).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot16).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot17).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot18).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot19).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot20).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot21).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot22).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot23).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot24).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot25).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot26).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot27).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot28).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot29).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot30).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot31).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot32).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot33).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot34).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot35).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot36).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot37).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot38).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot39).c_str(),50,PlayerFile);
      fgets(IntToStr(itemslot40).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot1).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot2).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot3).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot4).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot5).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot6).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot7).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot8).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot9).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot10).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot11).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot12).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot13).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot14).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot15).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot16).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot17).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot18).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot19).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot20).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot21).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot22).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot23).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot24).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot25).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot26).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot27).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot28).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot29).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot30).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot31).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot32).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot33).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot34).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot35).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot36).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot37).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot38).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot39).c_str(),50,PlayerFile);
      fgets(IntToStr(eitemslot40).c_str(),50,PlayerFile);
      // Mine/Forest/Fortress positions.
      fgets(IntToStr(MineX).c_str(),50,PlayerFile);
      fgets(IntToStr(MineY).c_str(),50,PlayerFile);
      fgets(IntToStr(MineZ).c_str(),50,PlayerFile);
      fgets(IntToStr(ForestX).c_str(),50,PlayerFile);
      fgets(IntToStr(ForestY).c_str(),50,PlayerFile);
      fgets(IntToStr(FortressX).c_str(),50,PlayerFile);
      fgets(IntToStr(FortressY).c_str(),50,PlayerFile);
      fgets(IntToStr(FortressZ).c_str(),50,PlayerFile);
      fgets(IntToStr(CashInBank).c_str(),50,PlayerFile);
      fgets(IntToStr(gold).c_str(),50,PlayerFile);
      fgets(IntToStr(gems).c_str(),50,PlayerFile);
      fgets(IntToStr(crystals).c_str(),50,PlayerFile);
}
Avatar of nfcdrummer
nfcdrummer
Flag of Australia image

ASKER

as you can see it is VERY wrong... an obvious display of my lack of programmingskill!! please note that the 'charstuff::writestats' function I shortened slightly so it would fit... it actually contains every single variable mentioned in the 'charstuff::readstats' function
Avatar of KangaRoo
KangaRoo

You can not use strings c_str() function to _change_ the string, only to read it. You compiler should give an error message, something like, 'cannot convert const char* to char*'

You would likely need some char buffer[1000], read into that and assign it to the string:

char buffer[1000];
fgets(buffer, 50, PlayerFile);
itemslot11 = string(buffer);

It should be ok to use the c_str() member for writing to the file.

There could be more problems, but the wrong usage of string::c_str() was catching the eye.
I'm sorrybut that doesn't answer ALL of my questions... Iknew it was wrong right from the start, but I needed to know how to do what I want to do PROPERLY before I look at fixing it... At the moment all I want to do is get it to work efficiently and properly

Basically I want to store a maximum of 200 chars details into 1 file, about 150 details for each char I would say.  All of the details so far for each character are on the 'charstuff::readstats();" functon.
ASKER CERTIFIED SOLUTION
Avatar of KangaRoo
KangaRoo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ohhhh I think I get you now.... I'll test it out...
but one thing,  when it saves the datait doesn't save each seperate part on a seperate line... how can I put an end-of-line indicator on each thing...
would that be for instance:

oh and by the way... all of those variables are actally integers... not strings or chars...

so that would be like this:

char buffer[1000];


strcpy(buffer,(char)itemslot11+"\n");
fputs(buffer,PlayerFile);


fgets(buffer,1000,PlayerFile)
itemslot11 = (int)buffer;


am I right?
hey hangon a sec.... I just found out some REAL infomation on file streams... I'll try them like I originally intended to, then I'll see what works best

thanks for your help though

hey well you're right...  I'm giving you the points to be fair... I'm too tired to think straitght, but you gave me a better idea of how to write it properly...
now I'm using filestreams insted of FILE*...

thanks heaps
>> strcpy(buffer,(char)itemslot11+"\n");

that cast (char)itemslot11 looks vey suspicious. What type is itemslot??

Indeed C++ iostreams are a better solution. I proposed the buffer idea just because it appeared to involve less modification work for you. I might have overlooked that iostreams are both easier to handle then FILE* i/o as well as safer in their type checking.

If you have any more questions, let me know.
itemslot1 through to 40 are all signed integers.  Some others are unsigned long integers, but I'm having trouble using those with the StrToInt/IntToStr commands.

If you could help me with that type I'd appreciate it.
The cast is completely out of place. So lets get on. Do you want to use FILE* file i/o or C++ file streams?
Well I've finished changing it all over to file streams...  It works fine except that it still doesn't add the newline characters...

and when you go to load a character it still doesn't show the name or anything I'll emailyou the entire code ok??

Please see if you can fix it up...  I think thats worth 150 points
Ok, my mail address is also in my profile (many experts do so): mailto:kangaroo@xs4all.nl
Mh, too bad, EE shoes http hyperlinks links but not e-mail addresses :(