Help with reading and writing to a file

Steve Torri storri at torri.org
Fri Jun 11 03:59:03 CEST 2004


On Fri, Jun 11, 2004 at 11:29:33AM +1000, Kevin Ryde wrote:
> Steve Torri <storri at torri.org> writes:
> >
> >> File_Writer class (attached)
> 
> That's much too complicated to guess at, you'll need to break out the
> operative part if you want help.

I am using the operator<< for writing the mpf_class objects to a
file. The file has been opened with a ofstream. So when I want to
write out a value I use the following macro:

#define FILE_OP(val,errstr) \
  { \
    m_file_handle << val; \
    std::cout << "pos: " << m_file_handle.tellp() << std::endl; \
    if (! File_Base::file_ready(&m_file_handle)) \
    { \
      std::stringstream error; \
      error << "Error writing: " << errstr << std::endl; \
      ACE_THROW(KURT_IOException(((error).str()).c_str())); \
    } \
  }

So a value like, mpf_class m_speed, would be written as so:

  FILE_OP(m_speed, "Writing speed value");

Now I do this for all mpf_class variables in the objects I am writing
to the file. At the end closing the file. When I read them from the
file I am using a similar macro which has opened the file with a
ifstream. The macro is as follows:

#define FILE_OP(val,errstr) \
  { \
    m_file_handle >> val; \
    std::cout << "pos: " << m_file_handle.tellg() << std::endl; \
    if (! File_Base::file_ready(&m_file_handle)) \
    { \
      std::stringstream error; \
      error << "Error reading: " << errstr << std::endl; \
      ACE_THROW(KURT_IOException(ACE_TEXT(((error).str()).c_str())));
  \
    } \
  }

So a mpf_class can be read from the file by doing:

  mpf_class input_speed;
  FILE_OP(input_speed, "Reading speed value");

This uses the operator>> for getting the value from the file.

So an example is one object that I write to a file contains two
mpf_class variables, m_speed and m_time. In a simple example that I
wrote to test the program I gave them a value;

  mpf_class m_speed (5000);
  mpf_class m_time (9000);

A Group_Set is initialized with values:

  kurt::Group_Set* gset = new kurt::Group_Set(speed,time);

So the Group_Set's variables m_speed = 5000 and m_time = 9000. So when
this is written to a file I should see a 5000 and a 9000 in the file
if I read it with hexedit. The reader which reads the values back from
the file uses the macros I described above. When I print out the
values that I have read from the file I get the following values:

  m_speed = 50009000
  m_time = 0

That is the problem I am having.

Stephen
-- 
Stephen Torri



More information about the gmp-discuss mailing list