problem saving files


I am trying to save data for characters for a game.

After I save the first one, which works fine, I create the second one and save. For some reason it writes over the first character with nothing, basically deleting it. The second one gets written, but at some offset nott matching the one I created by multiplying the size of the string by the number of the character created.

The following is the code:

int i,pos;

ofstream savefile;
pos= (sizeof(chrs[0]));
savefile.open ("save.txt",ios::out |ios::binary);


savefile.seekp (pos * r_cnt,ios::beg);
savefile.write((char *)(&chrs[r_cnt]), sizeof(chrs[0]));
r_cnt++;

Comments

  • :
    : I am trying to save data for characters for a game.
    :
    : After I save the first one, which works fine, I create the second one and save. For some reason it writes over the first character with nothing, basically deleting it. The second one gets written, but at some offset nott matching the one I created by multiplying the size of the string by the number of the character created.
    :
    : The following is the code:
    :
    : int i,pos;
    :
    : ofstream savefile;
    : pos= (sizeof(chrs[0]));
    : savefile.open ("save.txt",ios::out |ios::binary);
    :
    :
    : savefile.seekp (pos * r_cnt,ios::beg);
    : savefile.write((char *)(&chrs[r_cnt]), sizeof(chrs[0]));
    : r_cnt++;
    :


    you need to post more code for anyone tell what the problem is. what is variable chars? if it is a character array, then sizeof(chars[0]) will ALWAYS = 1 because sizeof(char) is always 1. So the pos will always = 1. what is r_cnt? what is its initial value? seekp(pos * r_cnt) == seekp(1 * r_cnt). when r_cnt == 0, seekp(1 * 0) = seekp(0), or seek to the beginning of the file.
  • The initial value is zero and r_cnt just keeps track of the number of characters in a roster.

    Oddly it used to work and I don't know what I changed to make it stop. I copied back the old code that used to work and no luck. I just went to my old PC to run it and it worked fine. I am now using windows XP and had 98 on my old one. Could that matter? Also I have made changes to the array by adding more data. I don't see how this could effect it, but is that possible?



    : :
    : : I am trying to save data for characters for a game.
    : :
    : : After I save the first one, which works fine, I create the second one and save. For some reason it writes over the first character with nothing, basically deleting it. The second one gets written, but at some offset nott matching the one I created by multiplying the size of the string by the number of the character created.
    : :
    : : The following is the code:
    : :
    : : int i,pos;
    : :
    : : ofstream savefile;
    : : pos= (sizeof(chrs[0]));
    : : savefile.open ("save.txt",ios::out |ios::binary);
    : :
    : :
    : : savefile.seekp (pos * r_cnt,ios::beg);
    : : savefile.write((char *)(&chrs[r_cnt]), sizeof(chrs[0]));
    : : r_cnt++;
    : :
    :
    :
    : you need to post more code for anyone tell what the problem is. what is variable chars? if it is a character array, then sizeof(chars[0]) will ALWAYS = 1 because sizeof(char) is always 1. So the pos will always = 1. what is r_cnt? what is its initial value? seekp(pos * r_cnt) == seekp(1 * r_cnt). when r_cnt == 0, seekp(1 * 0) = seekp(0), or seek to the beginning of the file.
    :

  • [red]savefile.open ("save.txt",ios::out |ios::binary);[/red]
    [purple]
    (experts please tell if i'm wrong.)

    when u open a file for writing [blue]without the append mode[/blue], all existing information from that file erased and file pointer indicator is set to the beginning of the file. so if u open a file every time to save every information, the file contents will be erased each time.
    [/purple]

    : The initial value is zero and r_cnt just keeps track of the number of characters in a roster.
    :
    : Oddly it used to work and I don't know what I changed to make it stop. I copied back the old code that used to work and no luck. I just went to my old PC to run it and it worked fine. I am now using windows XP and had 98 on my old one. Could that matter? Also I have made changes to the array by adding more data. I don't see how this could effect it, but is that possible?
    :
    :
    :
    : : :
    : : : I am trying to save data for characters for a game.
    : : :
    : : : After I save the first one, which works fine, I create the second one and save. For some reason it writes over the first character with nothing, basically deleting it. The second one gets written, but at some offset nott matching the one I created by multiplying the size of the string by the number of the character created.
    : : :
    : : : The following is the code:
    : : :
    : : : int i,pos;
    : : :
    : : : ofstream savefile;
    : : : pos= (sizeof(chrs[0]));
    : : : savefile.open ("save.txt",ios::out |ios::binary);
    : : :
    : : :
    : : : savefile.seekp (pos * r_cnt,ios::beg);
    : : : savefile.write((char *)(&chrs[r_cnt]), sizeof(chrs[0]));
    : : : r_cnt++;
    : : :
    : :
    : :
    : : you need to post more code for anyone tell what the problem is. what is variable chars? if it is a character array, then sizeof(chars[0]) will ALWAYS = 1 because sizeof(char) is always 1. So the pos will always = 1. what is r_cnt? what is its initial value? seekp(pos * r_cnt) == seekp(1 * r_cnt). when r_cnt == 0, seekp(1 * 0) = seekp(0), or seek to the beginning of the file.
    : :
    :
    :
    [hr][purple]~Donotalo()[/purple]

  • : [red]savefile.open ("save.txt",ios::out |ios::binary);[/red]
    : [purple]
    : (experts please tell if i'm wrong.)
    :
    : when u open a file for writing [blue]without the append mode[/blue], all existing information from that file erased and file pointer indicator is set to the beginning of the file. so if u open a file every time to save every information, the file contents will be erased each time.
    : [/purple]
    :

    you have to use ios::trunc to delete the contents of an existing file, but you are right that the file pointer is set to the beginning of the file. But he is calling seekp() to move it to where he wants it. We need to see more actual code in order to be very much help.

  • : : [red]savefile.open ("save.txt",ios::out |ios::binary);[/red]
    : : [purple]
    : : (experts please tell if i'm wrong.)
    : :
    : : when u open a file for writing [blue]without the append mode[/blue], all existing information from that file erased and file pointer indicator is set to the beginning of the file. so if u open a file every time to save every information, the file contents will be erased each time.
    : : [/purple]
    : :
    :
    : you have to use ios::trunc to delete the contents of an existing file, but you are right that the file pointer is set to the beginning of the file. But he is calling seekp() to move it to where he wants it. We need to see more actual code in order to be very much help.
    [purple]
    but the following code creates an empty text file (Dev C++ used):[code]
    int main() {
    char sample[] = "This is a test sample.";
    ofstream outfile;

    outfile.open("File.txt", ios::binary);
    outfile << sample;
    outfile.close();

    outfile.open("File.txt", ios::binary);
    //this causes to delete everything of the file (?) as far as i know

    outfile.close();

    system("PAUSE");
    return 0;
    }
    [/code]
    look at the OP's code:[code]
    [red]ofstream savefile;[/red]
    pos= (sizeof(chrs[0]));
    [red]savefile.open ("save.txt",ios::out |ios::binary);[/red]
    savefile.seekp (pos * r_cnt,ios::beg);
    savefile.write((char *)(&chrs[r_cnt]), sizeof(chrs[0]));
    r_cnt++;
    [/code]
    if s/he opens the file each time to save each information (suppose in an iteration) then always the file will be truncated to zero length.

    or am i wrong? probably [blue]ios::trunc[/blue] is used with [blue]ifstream[/blue]s? never used [blue]ios::trunc[/blue].
    [/purple]
    [hr][purple]~Donotalo()[/purple]

  • : [purple]
    : but the following code creates an empty text file (Dev C++ used):[/purple]

    You are right -- I was wrong.
  • : : [purple]
    : : but the following code creates an empty text file (Dev C++ used):[/purple]
    :
    : You are right -- I was wrong.
    :

    [code]

    void savenewchar()
    {
    int i,pos;
    roster_slot[r_cnt]=1; // r_cnt is the current # in the roster
    saved[0].roster_slot[r_cnt]=1; // saved[0] is a main game data array
    // has no effect on saving
    ofstream outfile;
    pos= (sizeof(chrs[0])); // size of array to be saved
    // also used to set pointer for next character.

    outfile.open ("save.txt",ios::out |ios::binary);

    outfile.seekp (pos * r_cnt);// tried ios::beg too.
    // on 1st pass seekp = 0, beginning of file
    // pos * 0 = 0 1st pass
    // pos * 1 = 300-not sure of exact # 2nd pass
    // pos * 2 = 600 3rd pass

    outfile.write((char *)(&chrs[r_cnt]), sizeof(chrs[0]));
    r_cnt++; // increases r_cnt for next creation of character

    outfile.close();

    main_menu();
    }

    [/code]

    I tried saving 2nd character to a seperate file. It worked but had the same strange offset.


    What is killing me is I used the exact same code before and compiled and it worked fine. It wasn't unitl a few days ago that it quit working properly.

    What could I have changed to do this?
    Is there a buffer I should clear, perhaps?

    I even through in some tests to check _rcnt and seekp before it writes and those are all in order too.



  • : [code]
    :
    : void savenewchar()
    : {
    : int i,pos;
    : roster_slot[r_cnt]=1; // r_cnt is the current # in the roster
    : saved[0].roster_slot[r_cnt]=1; // saved[0] is a main game data array
    : // has no effect on saving
    : ofstream outfile;
    : pos= (sizeof(chrs[0])); // size of array to be saved
    : // also used to set pointer for next character.
    :
    : [red]outfile.open ("save.txt",ios::out |ios::binary);[/red]
    :
    : outfile.seekp (pos * r_cnt);// tried ios::beg too.
    : // on 1st pass seekp = 0, beginning of file
    : // pos * 0 = 0 1st pass
    : // pos * 1 = 300-not sure of exact # 2nd pass
    : // pos * 2 = 600 3rd pass
    :
    : outfile.write((char *)(&chrs[r_cnt]), sizeof(chrs[0]));
    : r_cnt++; // increases r_cnt for next creation of character
    :
    : outfile.close();
    :
    : main_menu();
    : }
    :
    : [/code]
    :
    : I tried saving 2nd character to a seperate file. It worked but had the same strange offset.
    :
    :
    : What is killing me is I used the exact same code before and compiled and it worked fine. It wasn't unitl a few days ago that it quit working properly.
    :
    : What could I have changed to do this?
    : Is there a buffer I should clear, perhaps?
    :
    : I even through in some tests to check _rcnt and seekp before it writes and those are all in order too.
    [purple]

    as said before, u r deleting the contents of the file when u r opening it without using [blue]ios::ate[/blue] or [blue]ios::app[/blue] mode. try opening the file in either of the following two ways:
    [code]
    outfile.open ("save.txt", ios::ate |ios::binary);
    [/code]
    - OR -
    [code]
    outfile.open ("save.txt", ios::app |ios::binary);
    [/code]

    & DO NOT SEEK.

    [b][blue]ios::ate[/b][/blue]
    this mode opens the file and moves the file position indicator at the end of the file. however, u can still access anywhere in the file.

    [b][blue]ios::app[/b][/blue]
    this mode opens the file and any write operation takes place at the end of the file. ie, it appends at the end of the file whatever u write after opening the file (if u do not seek). this mode can only be used with [blue]ofstream[/blue]s.
    [/purple]
    [hr][purple]~Donotalo()[/purple]

  • Thanks alot,

    Strange though because, I didn't need ios::ate before, but I was using Borland compiler, now I am using Visual Studio.



    : : [code]
    : :
    : : void savenewchar()
    : : {
    : : int i,pos;
    : : roster_slot[r_cnt]=1; // r_cnt is the current # in the roster
    : : saved[0].roster_slot[r_cnt]=1; // saved[0] is a main game data array
    : : // has no effect on saving
    : : ofstream outfile;
    : : pos= (sizeof(chrs[0])); // size of array to be saved
    : : // also used to set pointer for next character.
    : :
    : : [red]outfile.open ("save.txt",ios::out |ios::binary);[/red]
    : :
    : : outfile.seekp (pos * r_cnt);// tried ios::beg too.
    : : // on 1st pass seekp = 0, beginning of file
    : : // pos * 0 = 0 1st pass
    : : // pos * 1 = 300-not sure of exact # 2nd pass
    : : // pos * 2 = 600 3rd pass
    : :
    : : outfile.write((char *)(&chrs[r_cnt]), sizeof(chrs[0]));
    : : r_cnt++; // increases r_cnt for next creation of character
    : :
    : : outfile.close();
    : :
    : : main_menu();
    : : }
    : :
    : : [/code]
    : :
    : : I tried saving 2nd character to a seperate file. It worked but had the same strange offset.
    : :
    : :
    : : What is killing me is I used the exact same code before and compiled and it worked fine. It wasn't unitl a few days ago that it quit working properly.
    : :
    : : What could I have changed to do this?
    : : Is there a buffer I should clear, perhaps?
    : :
    : : I even through in some tests to check _rcnt and seekp before it writes and those are all in order too.
    : [purple]
    :
    : as said before, u r deleting the contents of the file when u r opening it without using [blue]ios::ate[/blue] or [blue]ios::app[/blue] mode. try opening the file in either of the following two ways:
    : [code]
    : outfile.open ("save.txt", ios::ate |ios::binary);
    : [/code]
    : - OR -
    : [code]
    : outfile.open ("save.txt", ios::app |ios::binary);
    : [/code]
    :
    : & DO NOT SEEK.
    :
    : [b][blue]ios::ate[/b][/blue]
    : this mode opens the file and moves the file position indicator at the end of the file. however, u can still access anywhere in the file.
    :
    : [b][blue]ios::app[/b][/blue]
    : this mode opens the file and any write operation takes place at the end of the file. ie, it appends at the end of the file whatever u write after opening the file (if u do not seek). this mode can only be used with [blue]ofstream[/blue]s.
    : [/purple]
    : [hr][purple]~Donotalo()[/purple]
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

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

Categories