mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
rawBinary.hpp
Go to the documentation of this file.
1 /** \file rawBinary.hpp
2  * \author Jared R. Males (jaredmales@gmail.com)
3  * \brief Provides functions for working with raw binary files.
4  * \ingroup utils_files
5  *
6  */
7 
8 //***********************************************************************//
9 // Copyright 2018 Jared R. Males (jaredmales@gmail.com)
10 //
11 // This file is part of mxlib.
12 //
13 // mxlib is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // mxlib 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 mxlib. If not, see <http://www.gnu.org/licenses/>.
25 //***********************************************************************//
26 
27 #ifndef rawBinary_hpp
28 #define rawBinary_hpp
29 
30 #include <cstdio>
31 #include <string>
32 
33 #include "../mxlib.hpp"
34 #include "../mxException.hpp"
35 
36 namespace mx
37 {
38 namespace ioutils
39 {
40 
41 /// Read an array of data from a file as raw binary.
42 /** Here raw binary means no formatting or metadata.
43  *
44  * \ingroup ioutils
45  *
46  * \returns 0 on success
47  * \returns -1 on error
48  */
49 template<typename T>
50 int readRawBinary( T * data, ///< [out] the data pointer
51  size_t szData, ///< [in] number of elements of sizeof(T) to read
52  const std::string & fileName ///< [in] the file to read from
53  )
54 {
55  FILE * fout;
56 
57  fout = fopen(fileName.c_str(), "rb");
58  if(fout == 0)
59  {
60  mxThrowExceptionErrno(mx::err::fileoerr, errno, "readRawBinary", "Error from fopen [" + fileName + "]");
61  }
62 
63  int nrd = fread( data, sizeof(T), szData, fout);
64 
65  if(nrd != szData)
66  {
67  int en = errno; //get this before fclose
68  fclose(fout);
69  //Have to handle case where EOF reached but no error.
70  if(en != 0)
71  {
72  mxThrowExceptionErrno(mx::err::filererr, en, "readRawBinary", "Error from file [" + fileName + "]");
73  }
74  else
75  {
76  mxThrowException(mx::err::filererr,"readRawBinary", "Error reading from file, did not read all elements. [" + fileName+ "]");
77  }
78  }
79 
80  int res = fclose(fout);
81 
82  if(res != 0)
83  {
84  mxThrowExceptionErrno(mx::err::filecerr, errno, "readRawBinary", "Error closing file [" + fileName+ "]");
85  }
86 
87  return 0;
88 }
89 
90 /// Write an array of data to file as raw binary.
91 /** Here raw binary means no formatting or metadata,
92  * just the bytes pointed to by the array are written to
93  * disk.
94  *
95  * \ingroup ioutils
96  *
97  * \returns 0 on success
98  * \returns -1 on error
99  */
100 template<typename T>
101 int writeRawBinary( const std::string & fileName, ///< [in] the file to write to
102  T * data, ///< [in] the data pointer
103  size_t szData) ///< [in] number of elements of sizeof(T) to write
104 {
105  FILE * fout;
106 
107  fout = fopen(fileName.c_str(), "wb");
108  if(fout == 0)
109  {
110  mxThrowExceptionErrno(mx::err::fileoerr, errno, "writeRawBinary", "Error from fopen [" + fileName + "]");
111  }
112 
113 
114  int nwr = fwrite( data, sizeof(T), szData, fout);
115 
116  if(nwr != szData)
117  {
118  int en = errno; //save before close call
119  fclose(fout);
120 
121  //Have to handle case where EOF reached but no error.
122  if(en != 0)
123  {
124  mxThrowExceptionErrno(mx::err::filewerr, en, "writeRawBinary", "Error writing to file [" + fileName + "]");
125  }
126  else
127  {
128  mxThrowException(mx::err::filewerr, "writeRawBinary", "Error writing to file, did not write all elements. [" + fileName+ "]");
129  }
130 
131  }
132 
133  int res = fclose(fout);
134 
135  if(res != 0)
136  {
137  mxThrowExceptionErrno(mx::err::filecerr, errno, "writeRawBinary", "Error closing file [" + fileName+ "]");
138  }
139 
140  return 0;
141 }
142 
143 } //namespace ioutils
144 } //namespace mx
145 
146 #endif //rawBinary_hpp
mxException for errors closing a file
mxException for errors on opening a file
mxException for errors reading from a file
mxException for errors writing to a file
int writeRawBinary(const std::string &fileName, T *data, size_t szData)
Write an array of data to file as raw binary.
Definition: rawBinary.hpp:101
int readRawBinary(T *data, size_t szData, const std::string &fileName)
Read an array of data from a file as raw binary.
Definition: rawBinary.hpp:50
The mxlib c++ namespace.
Definition: mxError.hpp:107