Unstruct2D  1.0
Solution of 2-D Euler- and Navier-Stokes Equations on Unstructured Triangular Grids
 All Classes Files Functions Variables Typedefs Enumerations Macros
streamIO.h
Go to the documentation of this file.
1 //*****************************************************************************
6 //
7 // (c) J. Blazek, CFD Consulting & Analysis, www.cfd-ca.de
8 // Created February 15, 2014
9 // Last modification: June 30, 2014
10 //
11 //=============================================================================
12 //
13 // This program is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU General Public License
15 // as published by the Free Software Foundation; either version 2
16 // of the License, or (at your option) any later version.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 //
27 //*****************************************************************************
28 
29 #include <cstdlib>
30 #include <fstream>
31 #include <iostream>
32 #include <stdexcept>
33 #include <string>
34 
35 using std::ifstream;
36 using std::ofstream;
37 using std::runtime_error;
38 using std::string;
39 
47 inline string ReadLine( ifstream &stream )
48 {
49  string str;
50  string::size_type ic;
51 
52  if (stream.good())
53  {
54  getline( stream,str );
55 
56  // get rid of comment
57  ic = str.find_first_of( '#' );
58  if (ic != string::npos) str.erase( str.begin()+ic,str.end() );
59 
60  // get rid of leading spaces
61  ic = str.find_first_not_of( ' ' );
62  if ((int)ic > 0) str.erase( str.begin(),str.begin()+ic );
63 
64  // get rid of trailing spaces
65  ic = str.find_last_not_of( ' ' );
66  if (ic != string::npos) str.erase( str.begin()+ic+1,str.end() );
67 
68  }
69  else throw runtime_error( "could not read line from file." );
70 
71  return str;
72 }
73 
74 //*****************************************************************************
75 
82 template <class T> void ReadBinary( ifstream &stream, T &val )
83 {
84  if (stream.good())
85  stream.read( (char*) &val,sizeof(T) );
86  else
87  throw runtime_error( "could not read data from file." );
88 }
89 
90 //*****************************************************************************
91 
97 template <class T> void WriteBinary( ofstream &stream, const T &val )
98 {
99  stream.write( (char*) &val,sizeof(T) );
100 }
string ReadLine(ifstream &stream)
Definition: streamIO.h:47
void ReadBinary(ifstream &stream, T &val)
Definition: streamIO.h:82
void WriteBinary(ofstream &stream, const T &val)
Definition: streamIO.h:97