file overwriting/truncating

This is a discussion on file overwriting/truncating within the C++ Programming forums, part of the General Programming Boards category; i've got another problem hope that's the last one. I've been searching and trying diffrent think but i don't know ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    Question file overwriting/truncating

    i've got another problem hope that's the last one. I've been searching and trying diffrent think but i don't know what to do.

    I've got some array of classes stored in file

    Code:
    void Newmap(char file[]) //Create new map file
    {
      ofstream fout(file,ios::binary | ios::trunc);
      class Mapsqr Map[Map_Size][Map_Size];
      int coords[2];
      for (int i=0;i<Map_Size;i++)
      {
        for (int j=0;j<Map_Size;j++)
        {
          Map[i][j].SetParameters(i,j,Plains);
        }
      }
      fout.write((char*) &Map,sizeof Map);
      fout.close();
    }
    i can read it and everything is fine up to a point where i want to replace one Mapsqr class element in file.But i don't wont to read whole file and then rewrite it because it can be quit large. So i set a put pointer to corect place and wont to overwrite one element and end up with file with overwriten element end cutted off after it.

    Code:
    void EditOneTerrain (char file[],int coords[],TerrainType towhat)//edit terrain type
    {
      class Mapsqr tmp;
      ofstream fout(file,ios::binary);
      tmp.SetParameters(coords[X],coords[Y],towhat);
      for (int i=0;i<coords[X];i++)
      {
        fout.seekp(Map_Size*sizeof(tmp), ios::cur);
      }
    
      for (int i=0;i<coords[Y];i++)
      {
        fout.seekp(sizeof(tmp), ios::cur);
      }
      fout.write((char*) &tmp,sizeof tmp);
      system("pause");
      fout.close();
    }
    When i read the file there are all the elements up to the changed one, but after it file ends. What's wrong? Does write automaticaly truncate file after it?
    Last edited by baniakjr; 11-12-2006 at 05:31 AM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> Does write automaticaly truncate file after it?

    No. But the openmode you specified does. It's totally wack yo. here's the deal with openmodes.

    Code:
    filebuf *open(const char *filename,
        ios_base::openmode mode);
    
    The member function endeavors to open the file with filename filename, by calling fopen(filename, strmode). Here strmode is determined from mode & ~(ate | binary):
    
        * ios_base::in becomes "r" (open existing file for reading).
        * ios_base::out or ios_base::out | ios_base::trunc becomes "w" (truncate existing file or create for writing).
        * ios_base::out | ios_base::app becomes "a" (open existing file for appending all writes).
        * ios_base::in | ios_base::out becomes "r+" (open existing file for reading and writing).
        * ios_base::in | ios_base::out | ios_base::trunc becomes "w+" (truncate existing file or create for reading and writing).
        * ios_base::in | ios_base::out | ios_base::app becomes "a+" (open existing file for reading and for appending all writes).
    
    If mode & ios_base::binary is nonzero, the function appends b to strmode to open a binary stream instead of a text stream. It then stores the value returned by fopen in the file pointer fp. If mode & ios_base::ate is nonzero and the file pointer is not a null pointer, the function calls fseek(fp, 0, SEEK_END) to position the stream at end-of-file. If that positioning operation fails, the function calls close(fp) and stores a null pointer in the file pointer.
    
    If the file pointer is a null pointer, the function returns a null pointer. Otherwise, it returns this.
    I think you can specify the openmode ios::binary | ios::out | ios::in and it will work.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    Thanks.You're my hero.I was begining to warry that i'll have to change whol writing reading process. But now it works. Hmmm fun thing is that i was looking on openmodes but didn't manage to find anything. Thanks once again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21