C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
FSTREAM read/write Posted by Allegro_Dev on 22 Jul 2006 at 7:58 PM
I have a question about fstream
I am making a japanese language tutor program... (like math blaster)
and i am keeping it simple
struct KKR{ int x,y,w,h,bmp; }; // Kana/Kanji/Romanji KKR

struct LEVEL_DATA{
 KKR kana[50];
 KKR kanji[50];
 KKR romanji[50];
  string ON[50];
 string KUN[50];
 string ENG[50];
 char LEVEL_BMP[256];
}level;


I will be making files for each level ie: level_1.jbd

KKR will hold all the information for coordinats on the bitmap
with all the Kanji, Kana, Romanji text... becase i will later have them flying across the screen shooting at the right answer.. yada yada
this way file sizes are short.. loading the levels may take longer but not much.

the [50] after everything in the LEVEL_DATA represents EACH diffrent word

ie: if the user was learning KANJI and needed to select the right KANA
the program would see if(KANA[4] == KANJI[4]){ /* Correct answer */ }
if(KANA[4] == KANJI[2]){ /* Incorrect asnwer */ )

string is used becase char* was crashing at run-time while using
cin >> str;

this is where/when the user would need to enter the meaning of a KANJI
ie: the KANJI for 'water' was shown.. user would type in 'mizu' and mizu would be compared to KUN[?]

and LEVEL_BMP is the filename of the bitmap being used...

ok now you understand the struct
if it is the reason let me know

this is where the program fails:

    file.open(level_file, ios::trunc | ios::out);  
    file << rec_tot << " " << rec_num << " ";
    file.write((char*)&level,sizeof(LEVEL_DATA));
    file.close();
        // File is created....

    file.open(level_file, ios::in);
    file >> rec_tot;
    file >> rec_num; 
    file.read((char*)&level,sizeof(LEVEL_DATA));
    file.close();
        // File is loaded....


When i check my data i entered it is messed up:

i enter 1's 2's 3's etc for all the x,y,h,w,bmp of KKR's and a quick one word for the ON KUN ENG and then i view it on the screen...
it all looks good.. so i save it with the code above..
still looks good..
then i close program..
re-open program..
load same file..
ok so far so good..
view contents with same viewer.. and crash

i can't figure it out.. my code should be good... but... i cant figure out the DEV_C++ debuger...

if you want copy of my full code and my program that is crashing let me know i'll email
thank you

keeperols@comcast.net
Report
Re: FSTREAM read/write Posted by stober on 23 Jul 2006 at 11:42 AM
This message was edited by stober at 2006-7-23 11:50:40

: I have a question about fstream
: I am making a japanese language tutor program... (like math blaster)
: and i am keeping it simple
:
: struct KKR{ int x,y,w,h,bmp; }; // Kana/Kanji/Romanji KKR
: 
: struct LEVEL_DATA{
:  KKR kana[50];
:  KKR kanji[50];
:  KKR romanji[50];
:   string ON[50];
:  string KUN[50];
:  string ENG[50];
:  char LEVEL_BMP[256];
: }level;
: 

:
: I will be making files for each level ie: level_1.jbd
:
: KKR will hold all the information for coordinats on the bitmap
: with all the Kanji, Kana, Romanji text... becase i will later have them flying across the screen shooting at the right answer.. yada yada
: this way file sizes are short.. loading the levels may take longer but not much.
:
: the [50] after everything in the LEVEL_DATA represents EACH diffrent word
:
: ie: if the user was learning KANJI and needed to select the right KANA
: the program would see if(KANA[4] == KANJI[4]){ /* Correct answer */ }
: if(KANA[4] == KANJI[2]){ /* Incorrect asnwer */ )
:
: string is used becase char* was crashing at run-time while using
: cin >> str;
:
: this is where/when the user would need to enter the meaning of a KANJI
: ie: the KANJI for 'water' was shown.. user would type in 'mizu' and mizu would be compared to KUN[?]
:
: and LEVEL_BMP is the filename of the bitmap being used...
:
: ok now you understand the struct
: if it is the reason let me know
:
: this is where the program fails:
:
:
:     file.open(level_file, ios::trunc | ios::out);  
:     file << rec_tot << " " << rec_num << " ";
:     file.write((char*)&level,sizeof(LEVEL_DATA));
:     file.close();
:         // File is created....
: 
:     file.open(level_file, ios::in);
:     file >> rec_tot;
:     file >> rec_num; 
:     file.read((char*)&level,sizeof(LEVEL_DATA));
:     file.close();
:         // File is loaded....
: 

:
: When i check my data i entered it is messed up:
:
: i enter 1's 2's 3's etc for all the x,y,h,w,bmp of KKR's and a quick one word for the ON KUN ENG and then i view it on the screen...
: it all looks good.. so i save it with the code above..
: still looks good..
: then i close program..
: re-open program..
: load same file..
: ok so far so good..
: view contents with same viewer.. and crash
:
: i can't figure it out.. my code should be good... but... i cant figure out the DEV_C++ debuger...
:
: if you want copy of my full code and my program that is crashing let me know i'll email
: thank you
:
: keeperols@comcast.net
:

The file is opened in text mode, so if you are doing this on MS-Windows there may be a problem with the os interpreting some of the binary data in that structure. If you want to leave the file in text mode then read/write each of the structure fields individually and ignore the remainder of my comments below.


If you change the file type to binary mode then that structure cannot be written and read in one big swoop like you are attempting to do because all that is getting writting is the std::string class itself and not the actual text of the class object. And that is similar problem when you used char* instead of std::string. You can resolve this problem in a couple ways:

1) replace std::string with fixed-length char arrays. Here I just plugged in 255 as the length of the strings, but you can adjust that to whatever you think appropriate for your program. Then the write/read should work.

: struct LEVEL_DATA{
:  KKR kana[50];
:  KKR kanji[50];
:  KKR romanji[50];
   char ON[50][255];
   char KUN[50][255];
   char ENG[50][255];
:  char LEVEL_BMP[256];
: }level;


2) If you want to retain std::string in the structure you will have to make the file read/writes more complex. After writing the entire structure as you are doing now, you will have to write each of the individual strings.
file.write((char*)&level,sizeof(LEVEL_DATA));
// now write out the strings
for(int i = 0; i < 50; i++)
{
   file << level.ON[i] << "\n" << level.KUN[i] << "\n" << level.ENG[i] << "\n";
}

// reading do the same thing
file.read((char*)&level,sizeof(LEVEL_DATA));
// now read the strings.  Here use getline just in case the
// strings contains spaces.
for(int i = 0; i < 50; i++)
{
   getline(file,level.ON[i]);
   getline(file,level.KUN[i]);
   getline(file,level.ENG[i]);
}








Report
Re: FSTREAM read/write Posted by Allegro_Dev on 23 Jul 2006 at 12:05 PM

: 2) If you want to retain std::string in the structure you will have to make the file read/writes more complex. After writing the entire structure as you are doing now, you will have to write each of the individual strings.
:
: file.write((char*)&level,sizeof(LEVEL_DATA));
: // now write out the strings
: for(int i = 0; i < 50; i++)
: {
:    file << level.ON[i] << "\n" << level.KUN[i] << "\n" << level.ENG[i] << "\n";
: }
: 
: // reading do the same thing
: file.read((char*)&level,sizeof(LEVEL_DATA));
: // now read the strings.  Here use getline just in case the
: // strings contains spaces.
: for(int i = 0; i < 50; i++)
: {
:    getline(file,level.ON[i]);
:    getline(file,level.KUN[i]);
:    getline(file,level.ENG[i]);
: }
: 
: 

:
:

I tried above method and now the program will crash when i load the file
Report
Re: FSTREAM read/write Posted by stober on 23 Jul 2006 at 2:29 PM
: I tried above method and now the program will crash when i load the file
:


post code. I know what I wrote but I can't see what you wrote :)
Report
Re: FSTREAM read/write Posted by Allegro_Dev on 23 Jul 2006 at 4:02 PM

/*
 * LEVEL EDITOR
 * CONSOLE APP.
 *
 * Quick & easy Level creator for making .jbd files for (japblaster) temp name
 */

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct KKR{ int x,y,w,h,bmp; };

struct LEVEL_DATA{
 KKR kana[50];
 KKR kanji[50];
 KKR romanji[50];
 string ON[50];
 string KUN[50];
 string ENG[50];
 char LEVEL_BMP[256];
}level;

int rec_num, rec_tot;
char level_file[256];
fstream file;

void mod(){
  int i, inpt;
 cout << "\n\n\n\n\n -- MODIFY ------------------\n"
      << "Enter Record #: ";
  cin >> i;
  i--;
 cout << "\n\n\n\n Viewing record [" << i+1 << "]\n"
      << "Kana.x =   " << level.kana[i].x << "\n"
      << "Kana.y =   " << level.kana[i].y << "\n"
      << "Kana.w =   " << level.kana[i].w << "\n"
      << "Kana.h =   " << level.kana[i].h << "\n"
      << "Kana.bmp = " << level.kana[i].bmp << "\n\n"
      << "Kanji.x =   " << level.kanji[i].x << "\n"
      << "Kanji.y =   " << level.kanji[i].y << "\n"
      << "Kanji.w =   " << level.kanji[i].w << "\n"
      << "Kanji.h =   " << level.kanji[i].h << "\n"
      << "Kanji.bmp = " << level.kanji[i].bmp << "\n\n"
      << "Romanji.x =   " << level.romanji[i].x << "\n"
      << "Romanji.y =   " << level.romanji[i].y << "\n"
      << "Romanji.w =   " << level.romanji[i].w << "\n"
      << "Romanji.h =   " << level.romanji[i].h << "\n"
      << "Romanji.bmp = " << level.romanji[i].bmp << "\n\n"
      << "ON reading: "  << level.ON[i] << "\n"
      << "kun reading: " << level.KUN[i] << "\n"
      << "Eng meaning: " << level.ENG[i] << "\n\n"
      << "  (1) Modify This Record?\n"
      << "  (2) Cancel\n";
  cin >> inpt;
  
 if(inpt == 1){ 
 cout << "\n\n\n\n\nRecord : " << i+1 << "/50\n\n"
      << "Enter KANA:\n"
      << "  [X]: ";
  cin >> level.kana[i].x;
 cout << "  [Y]: ";
  cin >> level.kana[i].y;
 cout << "  [W]: ";
  cin >> level.kana[i].w;
 cout << "  [H]: ";
  cin >> level.kana[i].h;
 cout << "  [BMP ID]: ";
  cin >> level.kana[i].bmp;
 cout << "\nEnter KANJI:\n"
      << "  [X]: ";
  cin >> level.kanji[i].x;
 cout << "  [Y]: ";
  cin >> level.kanji[i].y;
 cout << "  [W]: ";
  cin >> level.kanji[i].w;
 cout << "  [H]: ";
  cin >> level.kanji[i].h;
 cout << "  [BMP ID]: ";
  cin >> level.kanji[i].bmp;
 cout << "\nEnter ROMANJI:\n"
      << "  [X]: ";
  cin >> level.romanji[i].x;
 cout << "  [Y]: ";
  cin >> level.romanji[i].y;
 cout << "  [W]: ";
  cin >> level.romanji[i].w;
 cout << "  [H]: ";
  cin >> level.romanji[i].h;
 cout << "  [BMP ID]: ";
  cin >> level.romanji[i].bmp;
 cout << "\nEnter ON reading: ";
  cin >> level.ON[i];
 cout << "\nEnter kun reading: ";
  cin >> level.KUN[i];
 cout << "\nEnter English meaning: ";
  cin >> level.ENG[i];
 cout << "\n\n Record Complete!\n\n";
 }
}

void add(){
 cout << "\n\n\n\n\n -- ADD ------------------"
      << " Record : " << rec_num+1 << "/50\n\n"
      << "Enter KANA:\n"
      << "  [X]: ";
  cin >> level.kana[rec_num].x;
 cout << "  [Y]: ";
  cin >> level.kana[rec_num].y;
 cout << "  [W]: ";
  cin >> level.kana[rec_num].w;
 cout << "  [H]: ";
  cin >> level.kana[rec_num].h;
 cout << "  [BMP ID]: ";
  cin >> level.kana[rec_num].bmp;
 cout << "\nEnter KANJI:\n"
      << "  [X]: ";
  cin >> level.kanji[rec_num].x;
 cout << "  [Y]: ";
  cin >> level.kanji[rec_num].y;
 cout << "  [W]: ";
  cin >> level.kanji[rec_num].w;
 cout << "  [H]: ";
  cin >> level.kanji[rec_num].h;
 cout << "  [BMP ID]: ";
  cin >> level.kanji[rec_num].bmp;
 cout << "\nEnter ROMANJI:\n"
      << "  [X]: ";
  cin >> level.romanji[rec_num].x;
 cout << "  [Y]: ";
  cin >> level.romanji[rec_num].y;
 cout << "  [W]: ";
  cin >> level.romanji[rec_num].w;
 cout << "  [H]: ";
  cin >> level.romanji[rec_num].h;
 cout << "  [BMP ID]: ";
  cin >> level.romanji[rec_num].bmp;
 cout << "\nEnter ON reading: ";
  cin >> level.ON[rec_num];
 cout << "\nEnter kun reading: ";
  cin >> level.KUN[rec_num];
 cout << "\nEnter English meaning: ";
  cin >> level.ENG[rec_num];
 cout << "\n\n Record Complete!\n\n\n\n\n";
  rec_num++;
  rec_tot++;
  if(rec_num > 49){ rec_num = 49; }
  if(rec_tot > 49){ rec_tot = 49; }
}

void clear(int I){
 level.kana[I].x = 0;
 level.kana[I].y = 0;
 level.kana[I].w = 0;
 level.kana[I].h = 0;
 level.kana[I].bmp = 0;
 level.kanji[I].x = 0;
 level.kanji[I].y = 0;
 level.kanji[I].w = 0;
 level.kanji[I].h = 0;
 level.kanji[I].bmp = 0;
 level.romanji[I].x = 0;
 level.romanji[I].y = 0;
 level.romanji[I].w = 0;
 level.romanji[I].h = 0;
 level.romanji[I].bmp = 0;
 level.ON[I] = "";
 level.KUN[I] = "";
 level.ENG[I] = "";
}

void del(){
 int inpt;
 int i = 0;
 bool loop = true;
 
 while(loop){
 cout << "\n\n\n\n Viewing record [" << i+1 << "]\n"
      << "Kana.x =   " << level.kana[i].x << "\n"
      << "Kana.y =   " << level.kana[i].y << "\n"
      << "Kana.w =   " << level.kana[i].w << "\n"
      << "Kana.h =   " << level.kana[i].h << "\n"
      << "Kana.bmp = " << level.kana[i].bmp << "\n"
      << "Kanji.x =   " << level.kanji[i].x << "\n"
      << "Kanji.y =   " << level.kanji[i].y << "\n"
      << "Kanji.w =   " << level.kanji[i].w << "\n"
      << "Kanji.h =   " << level.kanji[i].h << "\n"
      << "Kanji.bmp = " << level.kanji[i].bmp << "\n"
      << "Romanji.x =   " << level.romanji[i].x << "\n"
      << "Romanji.y =   " << level.romanji[i].y << "\n"
      << "Romanji.w =   " << level.romanji[i].w << "\n"
      << "Romanji.h =   " << level.romanji[i].h << "\n"
      << "Romanji.bmp = " << level.romanji[i].bmp << "\n"
      << "ON reading: "  << level.ON[i] << "\n"
      << "kun reading: " << level.KUN[i] << "\n"
      << "Eng meaning: " << level.ENG[i] << "\n"
      << "  (1) Continue"
      << "  (2) Stop"
      << "  (7) DELETE ";
  cin >> inpt;
  
  switch(inpt){
   case 1: loop=false; break;
   case 2: i++; break;
   case 7: clear(i); break;
   default: break;
  }
 }
 
}

void view(){
 int inpt;
 int i = 0;
 bool loop = true;
 
 while(loop){
 cout << "\n\n\n\n Viewing record [" << i+1 << "]\n"
      << "Kana.x =   " << level.kana[i].x << "\n"
      << "Kana.y =   " << level.kana[i].y << "\n"
      << "Kana.w =   " << level.kana[i].w << "\n"
      << "Kana.h =   " << level.kana[i].h << "\n"
      << "Kana.bmp = " << level.kana[i].bmp << "\n"
      << "Kanji.x =   " << level.kanji[i].x << "\n"
      << "Kanji.y =   " << level.kanji[i].y << "\n"
      << "Kanji.w =   " << level.kanji[i].w << "\n"
      << "Kanji.h =   " << level.kanji[i].h << "\n"
      << "Kanji.bmp = " << level.kanji[i].bmp << "\n"
      << "Romanji.x =   " << level.romanji[i].x << "\n"
      << "Romanji.y =   " << level.romanji[i].y << "\n"
      << "Romanji.w =   " << level.romanji[i].w << "\n"
      << "Romanji.h =   " << level.romanji[i].h << "\n"
      << "Romanji.bmp = " << level.romanji[i].bmp << "\n"
      << "ON reading: "  << level.ON[i] << "\n"
      << "kun reading: " << level.KUN[i] << "\n"
      << "Eng meaning: " << level.ENG[i] << "\n"
      << "  (1) Continue"
      << "  (2) Stop\n";
  cin >> inpt;
  
  switch(inpt){
   case 2: loop=false; break;
   case 1: i++; break;
   default: break;
  }
 }
}

void write(){
 char ch;

 cout << "\n\n\n\n\nAre you Sure you want to write to " << level_file << "?\n ALL DATA MAY BE LOST (y/n)";
  cin >> ch;
  
 switch(ch){
  case 'y':{
    file.open(level_file, ios::trunc | ios::out);  
    file << rec_tot << " " << rec_num << " ";
    file.write((char*)&level,sizeof(LEVEL_DATA));
    for(int i = 0; i < 50; i++){
     file << "\n" << level.ON[i] << "\n" << level.KUN[i] << "\n" << level.ENG[i];
    }
    file.close();   
  break;}
  case 'n': break;  
  default: break;
 }
}

void workit(){
 bool making = true;
 int input;
 
 while(making){
  cout << "\n\n\n\n\n\n\n\n"
       << "-- " << level_file << " -------------\n"
       << "Records:  " << rec_tot+1 << "/50\n"
       << "Current:  " << rec_num+1 << "\n\n"
       << "(1) Add\n"
       << "(2) Delete\n"
       << "(3) Modify\n"
       << "(4) View\n"
       << "(5) Save " << level_file << "\n\n"
       << "(9) Exit\n";
   cin >> input;
  
  switch(input){
   case 1: add(); break;
   case 2: del(); break;
   case 3: mod(); break;
   case 4: view(); break;
   case 5: write(); break;
   case 9: making = false; break;
   default:break;
  }
 }
}

void NEW(){
 rec_num = 0;
 
 cout << "\n\n\n\n\n __ NEW FILE __\nEnter File Name (.jbd): ";
  cin >> level_file;
 cout << "\n\n Enter File BITMAP assoc: ";
  cin >> level.LEVEL_BMP;
 workit();
}

void LOAD(){
 cout << "\n\n\n\n\n __ LOAD FILE __\nEnter File Name (.jbd): ";
 cin >> level_file;
 
 file.open(level_file, ios::in);
 file >> rec_tot;
 file >> rec_num; 
 file.read((char*)&level,sizeof(LEVEL_DATA));
 
 for(int i = 0; i < 50; i++){
  getline(file,level.ON[i]);
  getline(file,level.KUN[i]);
  getline(file,level.ENG[i]);
 }
 
 file.close();
 
 workit();
}

void main_menu(){
 bool zit = true;
 int i;
 
 while(zit){
  cout << "\n\n\n\n\n\n\nLEVEL CREATOR Main Menu\n\n"
       << " (1) New Level\n"
       << " (2) Load\n"
       << " (3) Quit\n";
   cin >> i;
   switch(i){
    case 1: NEW(); break;
    case 2: LOAD(); break;
    case 3: zit = false; break;
    default: break;
   }
 }
}

int main(){
 
 for(int i = 0; i < 50; i++){ clear(i); }
 
 main_menu();
 
 return 0;
}


Report
Re: FSTREAM read/write Posted by stober on 24 Jul 2006 at 7:17 AM
This message was edited by stober at 2006-7-24 8:4:40

several problems with your program:

1) after every integer input you must flush the input stream of the <Enter> key. One way to do this is to call cin.ignore().

2) You are attempting to mix binary and text modes in the file i/o. since most of the file is binary, open it in binary mode and write everything as binary data.

Here is the corrected problem
/*
 * LEVEL EDITOR
 * CONSOLE APP.
 *
 * Quick & easy Level creator for making .jbd files for (japblaster) temp name
 */

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct KKR{ int x,y,w,h,bmp; };

struct LEVEL_DATA{
 KKR kana[50];
 KKR kanji[50];
 KKR romanji[50];
 string ON[50];
 string KUN[50];
 string ENG[50];
 char LEVEL_BMP[256];
}level;

int rec_num, rec_tot;
char level_file[256];
fstream file;

void mod(){
  int i, inpt;
 cout << "\n\n\n\n\n -- MODIFY ------------------\n"
      << "Enter Record #: ";
  cin >> i; cin.ignore();
  i--;
 cout << "\n\n\n\n Viewing record [" << i+1 << "]\n"
      << "Kana.x =   " << level.kana[i].x << "\n"
      << "Kana.y =   " << level.kana[i].y << "\n"
      << "Kana.w =   " << level.kana[i].w << "\n"
      << "Kana.h =   " << level.kana[i].h << "\n"
      << "Kana.bmp = " << level.kana[i].bmp << "\n\n"
      << "Kanji.x =   " << level.kanji[i].x << "\n"
      << "Kanji.y =   " << level.kanji[i].y << "\n"
      << "Kanji.w =   " << level.kanji[i].w << "\n"
      << "Kanji.h =   " << level.kanji[i].h << "\n"
      << "Kanji.bmp = " << level.kanji[i].bmp << "\n\n"
      << "Romanji.x =   " << level.romanji[i].x << "\n"
      << "Romanji.y =   " << level.romanji[i].y << "\n"
      << "Romanji.w =   " << level.romanji[i].w << "\n"
      << "Romanji.h =   " << level.romanji[i].h << "\n"
      << "Romanji.bmp = " << level.romanji[i].bmp << "\n\n"
      << "ON reading: "  << level.ON[i] << "\n"
      << "kun reading: " << level.KUN[i] << "\n"
      << "Eng meaning: " << level.ENG[i] << "\n\n"
      << "  (1) Modify This Record?\n"
      << "  (2) Cancel\n";
  cin >> inpt; cin.ignore();
  
 if(inpt == 1){ 
 cout << "\n\n\n\n\nRecord : " << i+1 << "/50\n\n"
      << "Enter KANA:\n"
      << "  [X]: ";
  cin >> level.kana[i].x;cin.ignore();
 cout << "  [Y]: ";
  cin >> level.kana[i].y;cin.ignore();
 cout << "  [W]: ";
  cin >> level.kana[i].w;cin.ignore();
 cout << "  [H]: ";
  cin >> level.kana[i].h;cin.ignore();
 cout << "  [BMP ID]: ";
  cin >> level.kana[i].bmp;cin.ignore();
 cout << "\nEnter KANJI:\n"
      << "  [X]: ";
  cin >> level.kanji[i].x;cin.ignore();
 cout << "  [Y]: ";
  cin >> level.kanji[i].y;cin.ignore();
 cout << "  [W]: ";
  cin >> level.kanji[i].w;cin.ignore();
 cout << "  [H]: ";
  cin >> level.kanji[i].h;cin.ignore();
 cout << "  [BMP ID]: ";
  cin >> level.kanji[i].bmp;cin.ignore();
 cout << "\nEnter ROMANJI:\n"
      << "  [X]: ";
  cin >> level.romanji[i].x;cin.ignore();
 cout << "  [Y]: ";
  cin >> level.romanji[i].y;cin.ignore();
 cout << "  [W]: ";
  cin >> level.romanji[i].w;cin.ignore();
 cout << "  [H]: ";
  cin >> level.romanji[i].h;cin.ignore();
 cout << "  [BMP ID]: ";
  cin >> level.romanji[i].bmp;cin.ignore();
 cout << "\nEnter ON reading: ";
  cin >> level.ON[i];
 cout << "\nEnter kun reading: ";
  cin >> level.KUN[i];
 cout << "\nEnter English meaning: ";
  cin >> level.ENG[i];
 cout << "\n\n Record Complete!\n\n";
 }
}

void add(){
 cout << "\n\n\n\n\n -- ADD ------------------"
      << " Record : " << rec_num+1 << "/50\n\n"
      << "Enter KANA:\n"
      << "  [X]: ";
  cin >> level.kana[rec_num].x;
 cout << "  [Y]: ";
  cin >> level.kana[rec_num].y;
 cout << "  [W]: ";
  cin >> level.kana[rec_num].w;
 cout << "  [H]: ";
  cin >> level.kana[rec_num].h;
 cout << "  [BMP ID]: ";
  cin >> level.kana[rec_num].bmp;
 cout << "\nEnter KANJI:\n"
      << "  [X]: ";
  cin >> level.kanji[rec_num].x;
 cout << "  [Y]: ";
  cin >> level.kanji[rec_num].y;
 cout << "  [W]: ";
  cin >> level.kanji[rec_num].w;
 cout << "  [H]: ";
  cin >> level.kanji[rec_num].h;
 cout << "  [BMP ID]: ";
  cin >> level.kanji[rec_num].bmp;
 cout << "\nEnter ROMANJI:\n"
      << "  [X]: ";
  cin >> level.romanji[rec_num].x;
 cout << "  [Y]: ";
  cin >> level.romanji[rec_num].y;
 cout << "  [W]: ";
  cin >> level.romanji[rec_num].w;
 cout << "  [H]: ";
  cin >> level.romanji[rec_num].h;
 cout << "  [BMP ID]: ";
  cin >> level.romanji[rec_num].bmp;
 cout << "\nEnter ON reading: ";
  cin >> level.ON[rec_num];
 cout << "\nEnter kun reading: ";
  cin >> level.KUN[rec_num];
 cout << "\nEnter English meaning: ";
  cin >> level.ENG[rec_num];
 cout << "\n\n Record Complete!\n\n\n\n\n";
  rec_num++;
  rec_tot++;
  if(rec_num > 49){ rec_num = 49; }
  if(rec_tot > 49){ rec_tot = 49; }
}

void clear(int I){
 level.kana[I].x = 0;
 level.kana[I].y = 0;
 level.kana[I].w = 0;
 level.kana[I].h = 0;
 level.kana[I].bmp = 0;
 level.kanji[I].x = 0;
 level.kanji[I].y = 0;
 level.kanji[I].w = 0;
 level.kanji[I].h = 0;
 level.kanji[I].bmp = 0;
 level.romanji[I].x = 0;
 level.romanji[I].y = 0;
 level.romanji[I].w = 0;
 level.romanji[I].h = 0;
 level.romanji[I].bmp = 0;
 level.ON[I] = "";
 level.KUN[I] = "";
 level.ENG[I] = "";
}

void del(){
 int inpt;
 int i = 0;
 bool loop = true;
 
 while(loop){
 cout << "\n\n\n\n Viewing record [" << i+1 << "]\n"
      << "Kana.x =   " << level.kana[i].x << "\n"
      << "Kana.y =   " << level.kana[i].y << "\n"
      << "Kana.w =   " << level.kana[i].w << "\n"
      << "Kana.h =   " << level.kana[i].h << "\n"
      << "Kana.bmp = " << level.kana[i].bmp << "\n"
      << "Kanji.x =   " << level.kanji[i].x << "\n"
      << "Kanji.y =   " << level.kanji[i].y << "\n"
      << "Kanji.w =   " << level.kanji[i].w << "\n"
      << "Kanji.h =   " << level.kanji[i].h << "\n"
      << "Kanji.bmp = " << level.kanji[i].bmp << "\n"
      << "Romanji.x =   " << level.romanji[i].x << "\n"
      << "Romanji.y =   " << level.romanji[i].y << "\n"
      << "Romanji.w =   " << level.romanji[i].w << "\n"
      << "Romanji.h =   " << level.romanji[i].h << "\n"
      << "Romanji.bmp = " << level.romanji[i].bmp << "\n"
      << "ON reading: "  << level.ON[i] << "\n"
      << "kun reading: " << level.KUN[i] << "\n"
      << "Eng meaning: " << level.ENG[i] << "\n"
      << "  (1) Continue"
      << "  (2) Stop"
      << "  (7) DELETE ";
  cin >> inpt;
  
  switch(inpt){
   case 1: loop=false; break;
   case 2: i++; break;
   case 7: clear(i); break;
   default: break;
  }
 }
 
}

void view(){
 int inpt;
 int i = 0;
 bool loop = true;
 
 while(loop){
 cout << "\n\n\n\n Viewing record [" << i+1 << "]\n"
      << "Kana.x =   " << level.kana[i].x << "\n"
      << "Kana.y =   " << level.kana[i].y << "\n"
      << "Kana.w =   " << level.kana[i].w << "\n"
      << "Kana.h =   " << level.kana[i].h << "\n"
      << "Kana.bmp = " << level.kana[i].bmp << "\n"
      << "Kanji.x =   " << level.kanji[i].x << "\n"
      << "Kanji.y =   " << level.kanji[i].y << "\n"
      << "Kanji.w =   " << level.kanji[i].w << "\n"
      << "Kanji.h =   " << level.kanji[i].h << "\n"
      << "Kanji.bmp = " << level.kanji[i].bmp << "\n"
      << "Romanji.x =   " << level.romanji[i].x << "\n"
      << "Romanji.y =   " << level.romanji[i].y << "\n"
      << "Romanji.w =   " << level.romanji[i].w << "\n"
      << "Romanji.h =   " << level.romanji[i].h << "\n"
      << "Romanji.bmp = " << level.romanji[i].bmp << "\n"
      << "ON reading: "  << level.ON[i] << "\n"
      << "kun reading: " << level.KUN[i] << "\n"
      << "Eng meaning: " << level.ENG[i] << "\n"
      << "  (1) Continue"
      << "  (2) Stop\n";
  cin >> inpt;
  
  switch(inpt){
   case 2: loop=false; break;
   case 1: i++; break;
   default: break;
  }
 }
}

void write(){
 char ch;

 cout << "\n\n\n\n\nAre you Sure you want to write to " << level_file << "?\n ALL DATA MAY BE LOST (y/n)";
  cin >> ch;
  
 switch(ch){
  case 'y':{
	  file.open(level_file, ios::trunc | ios::out | ios::binary);  
    file.write((char*)&rec_tot,sizeof(rec_tot));
	file.write((char *)&rec_num,sizeof(rec_num));
    file.write((char*)&level,sizeof(LEVEL_DATA));
    for(int i = 0; i < 50; i++){
     file << level.ON[i] << "\r\n" << level.KUN[i] << "\r\n" << level.ENG[i] << "\r\n";
    }
    file.close();   
  break;}
  case 'n': break;  
  default: break;
 }
}

void workit(){
 bool making = true;
 int input;
 
 while(making){
  cout << "\n\n\n\n\n\n\n\n"
       << "-- " << level_file << " -------------\n"
       << "Records:  " << rec_tot+1 << "/50\n"
       << "Current:  " << rec_num+1 << "\n\n"
       << "(1) Add\n"
       << "(2) Delete\n"
       << "(3) Modify\n"
       << "(4) View\n"
       << "(5) Save " << level_file << "\n\n"
       << "(9) Exit\n";
   cin >> input;
  
  switch(input){
   case 1: add(); break;
   case 2: del(); break;
   case 3: mod(); break;
   case 4: view(); break;
   case 5: write(); break;
   case 9: making = false; break;
   default:break;
  }
 }
}

void NEW(){
 rec_num = 0;
 
 cout << "\n\n\n\n\n __ NEW FILE __\nEnter File Name (.jbd): ";
  cin >> level_file;
 cout << "\n\n Enter File BITMAP assoc: ";
  cin >> level.LEVEL_BMP;
 workit();
}

void LOAD(){
 cout << "\n\n\n\n\n __ LOAD FILE __\nEnter File Name (.jbd): ";
 cin >> level_file;
 
 file.open(level_file, ios::in | ios::binary);
 file.read((char*)&rec_tot, sizeof(rec_tot));
 file.read((char*)&rec_num,sizeof(rec_num)); 
 file.read((char*)&level,sizeof(LEVEL_DATA));
 
 for(int i = 0; i < 50; i++){
  getline(file,level.ON[i]);
  getline(file,level.KUN[i]);
  getline(file,level.ENG[i]);
 }
 
 file.close();
 
 workit();
}

void main_menu(){
 bool zit = true;
 int i;
 
 while(zit){
  cout << "\n\n\n\n\n\n\nLEVEL CREATOR Main Menu\n\n"
       << " (1) New Level\n"
       << " (2) Load\n"
       << " (3) Quit\n";
   cin >> i;
   switch(i){
    case 1: NEW(); break;
    case 2: LOAD(); break;
    case 3: zit = false; break;
    default: break;
   }
 }
}

int main(){
 
 for(int i = 0; i < 50; i++){ clear(i); }
 
 main_menu();
 
 return 0;
}





Report
Re: FSTREAM read/write Posted by Allegro_Dev on 24 Jul 2006 at 3:01 PM
HORRAY!!
you rock stober
i only lost my first string ON[] wheni loaded it.. but i bet i can take it from here

i didnt realize you needed to cin.ignore(); after that..
infact i havent seen that before... tho i dont iostream much
something more for me to reasearch thank you

&#12393;&#12358;&#12418;&#12354;&#12426;&#12364;&#12392;&#12372;&#12374;&#12356;&#12414;&#12377;&#65281;
thank you very much!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.