?
Solved

I need help with file access

Posted on 2000-04-20
13
Medium Priority
?
271 Views
Last Modified: 2010-04-10
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);
}
0
Comment
Question by:nfcdrummer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 

Author Comment

by:nfcdrummer
ID: 2733824
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
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2733869
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.
0
 

Author Comment

by:nfcdrummer
ID: 2733883
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Accepted Solution

by:
KangaRoo earned 450 total points
ID: 2733903
And the problem there is that you _can not use_ string::c_str() to modify the string, as I told you. You need a buffer, like I told you. As far as I can see you are using suitable C I/O functions, you just need that intermediate buffer.
If that is not the question then what?
0
 

Author Comment

by:nfcdrummer
ID: 2733913
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?
0
 

Author Comment

by:nfcdrummer
ID: 2733970
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

0
 

Author Comment

by:nfcdrummer
ID: 2734135
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
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2734792
>> 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.
0
 

Author Comment

by:nfcdrummer
ID: 2736348
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.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2737595
The cast is completely out of place. So lets get on. Do you want to use FILE* file i/o or C++ file streams?
0
 

Author Comment

by:nfcdrummer
ID: 2738887
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
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2739486
Ok, my mail address is also in my profile (many experts do so): mailto:kangaroo@xs4all.nl
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2739490
Mh, too bad, EE shoes http hyperlinks links but not e-mail addresses :(
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question