Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

FSTREAM read/write

Allegro_DevAllegro_Dev Posts: 23Member
I have a question about fstream
I am making a japanese language tutor program... (like math blaster)
and i am keeping it simple
[code]
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;
[/code]

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:

[code]
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....
[/code]

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

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2006-7-23 11:50:40[/red][/b][hr]
    : I have a question about fstream
    : I am making a japanese language tutor program... (like math blaster)
    : and i am keeping it simple
    : [code]
    : 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;
    : [/code]
    :
    : 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:
    :
    : [code]
    : 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....
    : [/code]
    :
    : 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.

    [code]
    : 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;
    [/code]

    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.
    [code]
    file.write((char*)&level,sizeof(LEVEL_DATA));
    // now write out the strings
    for(int i = 0; i < 50; i++)
    {
    file << level.ON[i] << "
    " << level.KUN[i] << "
    " << level.ENG[i] << "
    ";
    }

    // 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]);
    }

    [/code]






  • Allegro_DevAllegro_Dev Posts: 23Member

    : 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.
    : [code]
    : file.write((char*)&level,sizeof(LEVEL_DATA));
    : // now write out the strings
    : for(int i = 0; i < 50; i++)
    : {
    : file << level.ON[i] << "
    " << level.KUN[i] << "
    " << level.ENG[i] << "
    ";
    : }
    :
    : // 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]);
    : }
    :
    : [/code]
    :
    :

    I tried above method and now the program will crash when i load the file
  • stoberstober Posts: 9,765Member ✭✭✭
    : 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 :)
  • Allegro_DevAllegro_Dev Posts: 23Member
    [code]

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

    #include
    #include
    #include

    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 << "




    -- MODIFY ------------------
    "
    << "Enter Record #: ";
    cin >> i;
    i--;
    cout << "



    Viewing record [" << i+1 << "]
    "
    << "Kana.x = " << level.kana[i].x << "
    "
    << "Kana.y = " << level.kana[i].y << "
    "
    << "Kana.w = " << level.kana[i].w << "
    "
    << "Kana.h = " << level.kana[i].h << "
    "
    << "Kana.bmp = " << level.kana[i].bmp << "

    "
    << "Kanji.x = " << level.kanji[i].x << "
    "
    << "Kanji.y = " << level.kanji[i].y << "
    "
    << "Kanji.w = " << level.kanji[i].w << "
    "
    << "Kanji.h = " << level.kanji[i].h << "
    "
    << "Kanji.bmp = " << level.kanji[i].bmp << "

    "
    << "Romanji.x = " << level.romanji[i].x << "
    "
    << "Romanji.y = " << level.romanji[i].y << "
    "
    << "Romanji.w = " << level.romanji[i].w << "
    "
    << "Romanji.h = " << level.romanji[i].h << "
    "
    << "Romanji.bmp = " << level.romanji[i].bmp << "

    "
    << "ON reading: " << level.ON[i] << "
    "
    << "kun reading: " << level.KUN[i] << "
    "
    << "Eng meaning: " << level.ENG[i] << "

    "
    << " (1) Modify This Record?
    "
    << " (2) Cancel
    ";
    cin >> inpt;

    if(inpt == 1){
    cout << "




    Record : " << i+1 << "/50

    "
    << "Enter KANA:
    "
    << " [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 << "
    Enter KANJI:
    "
    << " [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 << "
    Enter ROMANJI:
    "
    << " [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 << "
    Enter ON reading: ";
    cin >> level.ON[i];
    cout << "
    Enter kun reading: ";
    cin >> level.KUN[i];
    cout << "
    Enter English meaning: ";
    cin >> level.ENG[i];
    cout << "

    Record Complete!

    ";
    }
    }

    void add(){
    cout << "




    -- ADD ------------------"
    << " Record : " << rec_num+1 << "/50

    "
    << "Enter KANA:
    "
    << " [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 << "
    Enter KANJI:
    "
    << " [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 << "
    Enter ROMANJI:
    "
    << " [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 << "
    Enter ON reading: ";
    cin >> level.ON[rec_num];
    cout << "
    Enter kun reading: ";
    cin >> level.KUN[rec_num];
    cout << "
    Enter English meaning: ";
    cin >> level.ENG[rec_num];
    cout << "

    Record Complete!




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



    Viewing record [" << i+1 << "]
    "
    << "Kana.x = " << level.kana[i].x << "
    "
    << "Kana.y = " << level.kana[i].y << "
    "
    << "Kana.w = " << level.kana[i].w << "
    "
    << "Kana.h = " << level.kana[i].h << "
    "
    << "Kana.bmp = " << level.kana[i].bmp << "
    "
    << "Kanji.x = " << level.kanji[i].x << "
    "
    << "Kanji.y = " << level.kanji[i].y << "
    "
    << "Kanji.w = " << level.kanji[i].w << "
    "
    << "Kanji.h = " << level.kanji[i].h << "
    "
    << "Kanji.bmp = " << level.kanji[i].bmp << "
    "
    << "Romanji.x = " << level.romanji[i].x << "
    "
    << "Romanji.y = " << level.romanji[i].y << "
    "
    << "Romanji.w = " << level.romanji[i].w << "
    "
    << "Romanji.h = " << level.romanji[i].h << "
    "
    << "Romanji.bmp = " << level.romanji[i].bmp << "
    "
    << "ON reading: " << level.ON[i] << "
    "
    << "kun reading: " << level.KUN[i] << "
    "
    << "Eng meaning: " << level.ENG[i] << "
    "
    << " (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 << "



    Viewing record [" << i+1 << "]
    "
    << "Kana.x = " << level.kana[i].x << "
    "
    << "Kana.y = " << level.kana[i].y << "
    "
    << "Kana.w = " << level.kana[i].w << "
    "
    << "Kana.h = " << level.kana[i].h << "
    "
    << "Kana.bmp = " << level.kana[i].bmp << "
    "
    << "Kanji.x = " << level.kanji[i].x << "
    "
    << "Kanji.y = " << level.kanji[i].y << "
    "
    << "Kanji.w = " << level.kanji[i].w << "
    "
    << "Kanji.h = " << level.kanji[i].h << "
    "
    << "Kanji.bmp = " << level.kanji[i].bmp << "
    "
    << "Romanji.x = " << level.romanji[i].x << "
    "
    << "Romanji.y = " << level.romanji[i].y << "
    "
    << "Romanji.w = " << level.romanji[i].w << "
    "
    << "Romanji.h = " << level.romanji[i].h << "
    "
    << "Romanji.bmp = " << level.romanji[i].bmp << "
    "
    << "ON reading: " << level.ON[i] << "
    "
    << "kun reading: " << level.KUN[i] << "
    "
    << "Eng meaning: " << level.ENG[i] << "
    "
    << " (1) Continue"
    << " (2) Stop
    ";
    cin >> inpt;

    switch(inpt){
    case 2: loop=false; break;
    case 1: i++; break;
    default: break;
    }
    }
    }

    void write(){
    char ch;

    cout << "




    Are you Sure you want to write to " << level_file << "?
    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 << "
    " << level.ON[i] << "
    " << level.KUN[i] << "
    " << level.ENG[i];
    }
    file.close();
    break;}
    case 'n': break;
    default: break;
    }
    }

    void workit(){
    bool making = true;
    int input;

    while(making){
    cout << "







    "
    << "-- " << level_file << " -------------
    "
    << "Records: " << rec_tot+1 << "/50
    "
    << "Current: " << rec_num+1 << "

    "
    << "(1) Add
    "
    << "(2) Delete
    "
    << "(3) Modify
    "
    << "(4) View
    "
    << "(5) Save " << level_file << "

    "
    << "(9) Exit
    ";
    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 << "




    __ NEW FILE __
    Enter File Name (.jbd): ";
    cin >> level_file;
    cout << "

    Enter File BITMAP assoc: ";
    cin >> level.LEVEL_BMP;
    workit();
    }

    void LOAD(){
    cout << "




    __ LOAD FILE __
    Enter 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 << "






    LEVEL CREATOR Main Menu

    "
    << " (1) New Level
    "
    << " (2) Load
    "
    << " (3) Quit
    ";
    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;
    }

    [/code]
  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2006-7-24 8:4:40[/red][/b][hr]
    several problems with your program:

    1) after every integer input you must flush the input stream of the 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
    [code]
    /*
    * LEVEL EDITOR
    * CONSOLE APP.
    *
    * Quick & easy Level creator for making .jbd files for (japblaster) temp name
    */

    #include
    #include
    #include

    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 << "




    -- MODIFY ------------------
    "
    << "Enter Record #: ";
    cin >> i; cin.ignore();
    i--;
    cout << "



    Viewing record [" << i+1 << "]
    "
    << "Kana.x = " << level.kana[i].x << "
    "
    << "Kana.y = " << level.kana[i].y << "
    "
    << "Kana.w = " << level.kana[i].w << "
    "
    << "Kana.h = " << level.kana[i].h << "
    "
    << "Kana.bmp = " << level.kana[i].bmp << "

    "
    << "Kanji.x = " << level.kanji[i].x << "
    "
    << "Kanji.y = " << level.kanji[i].y << "
    "
    << "Kanji.w = " << level.kanji[i].w << "
    "
    << "Kanji.h = " << level.kanji[i].h << "
    "
    << "Kanji.bmp = " << level.kanji[i].bmp << "

    "
    << "Romanji.x = " << level.romanji[i].x << "
    "
    << "Romanji.y = " << level.romanji[i].y << "
    "
    << "Romanji.w = " << level.romanji[i].w << "
    "
    << "Romanji.h = " << level.romanji[i].h << "
    "
    << "Romanji.bmp = " << level.romanji[i].bmp << "

    "
    << "ON reading: " << level.ON[i] << "
    "
    << "kun reading: " << level.KUN[i] << "
    "
    << "Eng meaning: " << level.ENG[i] << "

    "
    << " (1) Modify This Record?
    "
    << " (2) Cancel
    ";
    cin >> inpt; cin.ignore();

    if(inpt == 1){
    cout << "




    Record : " << i+1 << "/50

    "
    << "Enter KANA:
    "
    << " [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 << "
    Enter KANJI:
    "
    << " [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 << "
    Enter ROMANJI:
    "
    << " [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 << "
    Enter ON reading: ";
    cin >> level.ON[i];
    cout << "
    Enter kun reading: ";
    cin >> level.KUN[i];
    cout << "
    Enter English meaning: ";
    cin >> level.ENG[i];
    cout << "

    Record Complete!

    ";
    }
    }

    void add(){
    cout << "




    -- ADD ------------------"
    << " Record : " << rec_num+1 << "/50

    "
    << "Enter KANA:
    "
    << " [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 << "
    Enter KANJI:
    "
    << " [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 << "
    Enter ROMANJI:
    "
    << " [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 << "
    Enter ON reading: ";
    cin >> level.ON[rec_num];
    cout << "
    Enter kun reading: ";
    cin >> level.KUN[rec_num];
    cout << "
    Enter English meaning: ";
    cin >> level.ENG[rec_num];
    cout << "

    Record Complete!




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



    Viewing record [" << i+1 << "]
    "
    << "Kana.x = " << level.kana[i].x << "
    "
    << "Kana.y = " << level.kana[i].y << "
    "
    << "Kana.w = " << level.kana[i].w << "
    "
    << "Kana.h = " << level.kana[i].h << "
    "
    << "Kana.bmp = " << level.kana[i].bmp << "
    "
    << "Kanji.x = " << level.kanji[i].x << "
    "
    << "Kanji.y = " << level.kanji[i].y << "
    "
    << "Kanji.w = " << level.kanji[i].w << "
    "
    << "Kanji.h = " << level.kanji[i].h << "
    "
    << "Kanji.bmp = " << level.kanji[i].bmp << "
    "
    << "Romanji.x = " << level.romanji[i].x << "
    "
    << "Romanji.y = " << level.romanji[i].y << "
    "
    << "Romanji.w = " << level.romanji[i].w << "
    "
    << "Romanji.h = " << level.romanji[i].h << "
    "
    << "Romanji.bmp = " << level.romanji[i].bmp << "
    "
    << "ON reading: " << level.ON[i] << "
    "
    << "kun reading: " << level.KUN[i] << "
    "
    << "Eng meaning: " << level.ENG[i] << "
    "
    << " (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 << "



    Viewing record [" << i+1 << "]
    "
    << "Kana.x = " << level.kana[i].x << "
    "
    << "Kana.y = " << level.kana[i].y << "
    "
    << "Kana.w = " << level.kana[i].w << "
    "
    << "Kana.h = " << level.kana[i].h << "
    "
    << "Kana.bmp = " << level.kana[i].bmp << "
    "
    << "Kanji.x = " << level.kanji[i].x << "
    "
    << "Kanji.y = " << level.kanji[i].y << "
    "
    << "Kanji.w = " << level.kanji[i].w << "
    "
    << "Kanji.h = " << level.kanji[i].h << "
    "
    << "Kanji.bmp = " << level.kanji[i].bmp << "
    "
    << "Romanji.x = " << level.romanji[i].x << "
    "
    << "Romanji.y = " << level.romanji[i].y << "
    "
    << "Romanji.w = " << level.romanji[i].w << "
    "
    << "Romanji.h = " << level.romanji[i].h << "
    "
    << "Romanji.bmp = " << level.romanji[i].bmp << "
    "
    << "ON reading: " << level.ON[i] << "
    "
    << "kun reading: " << level.KUN[i] << "
    "
    << "Eng meaning: " << level.ENG[i] << "
    "
    << " (1) Continue"
    << " (2) Stop
    ";
    cin >> inpt;

    switch(inpt){
    case 2: loop=false; break;
    case 1: i++; break;
    default: break;
    }
    }
    }

    void write(){
    char ch;

    cout << "




    Are you Sure you want to write to " << level_file << "?
    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] << "
    " << level.KUN[i] << "
    " << level.ENG[i] << "
    ";
    }
    file.close();
    break;}
    case 'n': break;
    default: break;
    }
    }

    void workit(){
    bool making = true;
    int input;

    while(making){
    cout << "







    "
    << "-- " << level_file << " -------------
    "
    << "Records: " << rec_tot+1 << "/50
    "
    << "Current: " << rec_num+1 << "

    "
    << "(1) Add
    "
    << "(2) Delete
    "
    << "(3) Modify
    "
    << "(4) View
    "
    << "(5) Save " << level_file << "

    "
    << "(9) Exit
    ";
    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 << "




    __ NEW FILE __
    Enter File Name (.jbd): ";
    cin >> level_file;
    cout << "

    Enter File BITMAP assoc: ";
    cin >> level.LEVEL_BMP;
    workit();
    }

    void LOAD(){
    cout << "




    __ LOAD FILE __
    Enter 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 << "






    LEVEL CREATOR Main Menu

    "
    << " (1) New Level
    "
    << " (2) Load
    "
    << " (3) Quit
    ";
    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;
    }

    [/code]



  • Allegro_DevAllegro_Dev Posts: 23Member
    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

    どうもありがとございます!
    thank you very much!
Sign In or Register to comment.