C++/c

Advertisement


Question
class FloatFileParameters
  {
  public:
      explicit FloatFileParameters( char const * filename )
      : iStrm( filename )
      {
      }

      ~FloatFileParameters()
      {
         iStrm.close();
      }

      float GetValue()
      {
         float value(0.0);
         iStrm >> value;
         return value;
      }

  private:
      std::ifstream      iStrm;
  };
if i use this program then where are those values get stored.

Answer
Please read the rest of the answer from which this class (a type not a program) was an example. If you do then you will see the usage explained below where this class is introduced:

"The above is very basic but shows the idea. It can be used as below:

   void SomeFunction()
   {
       FloatFileParameters floatParams( "coefficients.dat" );
       float param1( floatParams.GetValue() );
       float param2( floatParams.GetValue() );
       float param3( floatParams.GetValue() );
   } // floatParams goes out of scope, is destroyed and
     // destructor closes file stream.
"
Instances of the class are used to wrap up the process of reading the values from a file. Each call to the GetValue member function _returns_ the value read from the file and so can be used to initialise or assign to other float objects as in the SomeFunction example I quote from my original answer above where the read values returned from the floatParams.GetValue calls are stored in param1, param2 and param3 respectfully. Of course these values could just as easily be read using a loop with the values being assigned to some collection:

   float params[10];
   for ( int i=0; i < 10; ++i )
   {
       params[i] = floatParams.GetValue();
   }

Where you store the values returned from the GetValue member function depends on your particular requirements. I am sorry but as I do not have the whole design of your application (and am not interested in it) I cannot say where you should store the coefficients presumably you have these already and just need the values assigned to them from those in the file.

I would remind you that the code I show in my answers is for example purposes _only_ and is NOT production grade code. For example the above code runs into problems if the file does not contain at least 10 properly formatted float value strings and the FloatFileParameters class would need some way of allowing states such as open, fail, bad and eof (end of file) on the stream to be made available to users in some form.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.