Reading from file.But only part of it.

This is a discussion on Reading from file.But only part of it. within the C++ Programming forums, part of the General Programming Boards category; Ok i've got some problem.I have function that saves array of class objects to a file using write function from ...

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

    Question Reading from file.But only part of it.

    Ok i've got some problem.I have function that saves array of class objects to a file using write function from fstream.h

    Code:
    #include<iostream.h>
    #include<fstream.h>
    #include"Mapsqr.h"
    
    void Newmap(char file[])
    {
      ofstream fout(file,ios::binary);
      class Mapsqr Map[100][100];
      fout.write((char*) &Map,sizeof Map);
      fout.close();
    }
    Now i want to read from that file but not the whole 100x100 array but only an smaller one let say 20 on 20 such that it would be elements from squer map[51][23] to map [71][53].

    if i make it like this

    Code:
    void Readmap(char file[])
    {
      class Mapsqr tmpmap[20][20];
      ifstream fin(file,ios::binary);
      fin.read((char*) &tmpmap,sizeof tmpmap);
      fin.close();
    }
    then tmpmap[0][0] will be element that was in map[0][0] but at tmpmap[1][0] i will have element that was map[0][20] not map[1][0].

    so how to make it such way that tmpmap[1][0] will have same data as map[1][0]? How to tell program from where to read. Do i have to do such thing to get map[0][67] element or is there some way to tell program where to begin to read?

    Code:
    void Readelementmap(char file[])
    {
      class Mapsqr tmp;
      ifstream fin(file,ios::binary);
      for (int i=0;i<67;i++)
      {
        fin.read((char*) &tmp,sizeof tmp);
      }
      fin.close();
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One idea would be to use seekg().
    Code:
    void Readelementmap(char file[])
    {
      class Mapsqr tmp;
      ifstream fin(file, ios::binary);
      for (int i=0; i<20; i++)
        for (int j=0; j<20; j++)
        {
          //Read 20 Mapsqr
          fin.read((char*) &tmp, sizeof(tmp));
        }
        //Skip 80 Mapsqr
        fin.seekg(80*sizeof(tmp), ios::cur);
      }
      fin.close();
    }
    Last edited by swoopy; 11-08-2006 at 08:13 PM.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Or you could read 20 Mapsqr at a time.
    Code:
      class Mapsqr tmpmap[20][20];
    
      ifstream fin(file, ios::binary);
      for (int i=0; i<20; i++)
      {
        //Read 20 Mapsqr
        fin.read((char *) &tmpmap[i], sizeof(tmpmap[i]);
        //Skip 80 Mapsqr
        fin.seekg(80*sizeof(Mapsqr), ios::cur);
      }
    Last edited by swoopy; 11-08-2006 at 08:25 PM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Actually now that I've thought about it, you need some offsets also as you're reading. The code above was editted to calculate the offset. Hopefully I got them close to right.
    Last edited by swoopy; 11-08-2006 at 05:44 PM.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Ok, I edited the code above again. Whatever you do, to skip Mapsqr's in the file, you can use seekg(). Or you can do what you suggested, and simply read one Mapsqr at a time.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    Hmmm.thanks that sounds just fine.At the begining i was worry about not taking under consideration the size of Mapsqr when using seekg().Thanks a lot.I'll try it when i'll have some time

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > class Mapsqr Map[100][100];
    > fout.write((char*) &Map,sizeof Map);
    ..
    > class Mapsqr tmpmap[20][20];
    > fin.read((char*) &tmpmap,sizeof tmpmap);
    If you're trying to read the 'top-left' corner of the map, then this is the wrong thing to do.

    At best, you get the first 4 complete rows of the map you wrote out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading a file
    By nhubred in forum C++ Programming
    Replies: 3
    Last Post: 05-21-2009, 11:34 AM
  2. Read part of file
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 04-13-2009, 01:51 PM
  3. How to use this part of dirent.h
    By kermit in forum Linux Programming
    Replies: 2
    Last Post: 01-31-2009, 07:51 AM
  4. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  5. !!!Urgent Help on a group project!!!!!
    By AmeenR in forum C Programming
    Replies: 3
    Last Post: 12-13-2003, 08:22 PM

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