SDXFrameWork  0.04
SDXFrameWork
 全て クラス ネームスペース 関数 変数 ページ
File.h
1 #pragma once
2 #include <fstream>
3 #include <sstream>
4 #include <iostream>
5 
6 namespace SDX
7 {
9 enum class FileMode
10 {
11  Read,//読込のみ
12  Write,//書込のみ
13  Add,//書込時、末尾に追加
14  None,//開かれていない
15 };
16 
18 class File
20 {
21 private:
22  std::ifstream fst;
23  bool canRead;
24  bool canWrite;
25  bool canAdd;
26  bool isBinary;
27  std::string fileName;
28 public:
29 
31  File(const char* ファイル名 , FileMode 読み書きモード , bool バイナリファイル = false )
32  {
33  File::Open( ファイル名 , 読み書きモード , バイナリファイル );
34  }
35 
37  bool Open(const char* ファイル名 , FileMode 読み書きモード , bool バイナリファイル = false )
38  {
39  std::string mode;
40  this->fileName = ファイル名;
41 
42  switch(読み書きモード)
43  {
44  case FileMode::Read:
45  if( isBinary ) fst.open( ファイル名 , std::ios::in | std::ios::binary );
46  else fst.open( ファイル名 , std::ios::in );
47  canRead = true;
48  canWrite = false;
49  canAdd = false;
50  break;
51  case FileMode::Write:
52  if( isBinary ) fst.open( ファイル名 , std::ios::out | std::ios::binary );
53  else fst.open( ファイル名 , std::ios::out );
54  canWrite = true;
55  canRead = false;
56  canAdd = false;
57  break;
58  case FileMode::Add:
59  if( isBinary ) fst.open( ファイル名 , std::ios::out | std::ios::app | std::ios::binary );
60  else fst.open( ファイル名 , std::ios::out | std::ios::app );
61  canWrite = true;
62  canAdd = true;
63  canRead = false;
64  break;
65  }
66 
67  this->isBinary = バイナリファイル;
68 
69  if ( fst.fail() )
70  {
71  canRead = false;
72  canWrite = false;
73  canAdd = false;
74  }
75 
76  return true;
77  }
78 
80  bool Close()
81  {
82  fst.close();
83  canRead = false;
84  canWrite = false;
85  canAdd = false;
86  }
87 
89  FileMode GetFileMode()
90  {
91  if( this->canAdd ) return FileMode::Add;
92  if( this->canWrite) return FileMode::Write;
93  if( this->canRead ) return FileMode::Read;
94  return FileMode::None;
95  }
96 
98  const char* GetFileName()
99  {
100  return this->fileName.c_str();
101  }
102 
104  template< class T>
107  bool Read(T &読込先変数 )
108  {
109  if( canRead ) fst << 読込先変数;
110  return canRead;
111  }
112 
114  template< class T>
117  bool Write(T &書込元変数)
118  {
119  if( canWrite ) fst >> 書込元変数;
120  return canWrite;
121  }
122 
124  template< class T>
125  bool ReadWrite(T &読み書き変数 )
126  {
127  if( canRead )
128  {
129  fst >> buf;
130  return true;
131  }else if( canWrite ){
132  fst << buf;
133  return true;
134  }
135  return false;
136  }
137 
139  bool GetLine(std::string &代入先文字列)
141  {
142  if (canRead) getline(fst, 代入先文字列);
143  return canRead;
144  }
145 
147  bool GetLineCSV(std::vector<std::string> &代入先配列)
150  {
151  if( canRead )
152  {
153  std::string str;
154  std::string buf;
155 
156  getline( fst , str );
157  std::istringstream iss( str );
158 
159  while( getline( iss , buf , ',' ) )
160  {
161  代入先配列.push_back(buf);
162  }
163  }
164  return canRead;
165  }
166 
168  bool CheckEOF()
169  {
170  return fst.eof();
171  }
172 
173 };
174 }
bool CheckEOF()
ファイルの終端判定.
Definition: File.h:168
bool Read(T &読込先変数)
データを読み込む.
Definition: File.h:107
bool Write(T &書込元変数)
データを書き込む.
Definition: File.h:117
const char * GetFileName()
ファイル名を取得.
Definition: File.h:98
bool Open(const char *ファイル名, FileMode 読み書きモード, bool バイナリファイル=false)
ファイルを開く.
Definition: File.h:37
bool GetLine(std::string &代入先文字列)
ファイルから一行読込.
Definition: File.h:140
FileMode GetFileMode()
ファイルモードを取得.
Definition: File.h:89
bool GetLineCSV(std::vector< std::string > &代入先配列)
CSVファイルの一行読込.
Definition: File.h:149
bool Close()
ファイルを閉じる.
Definition: File.h:80
bool ReadWrite(T &読み書き変数)
FileModeがReadの場合Read、WriteかAddの場合Writeを行う.
Definition: File.h:125
File(const char *ファイル名, FileMode 読み書きモード, bool バイナリファイル=false)
ファイル名とモードを指定して、ファイルを開く.
Definition: File.h:31