回答編集履歴

1

a

2018/10/14 12:55

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- ## 手順
5
+ ## 手順 (Python)
6
6
 
7
7
 
8
8
 
@@ -59,3 +59,147 @@
59
59
  # Python の float 型は 8bytes (倍精度) なので、4 * 50 * 50 = 20000
60
60
 
61
61
  ```
62
+
63
+
64
+
65
+ ## C++
66
+
67
+
68
+
69
+ すいません。タイトルを見てませんでした。
70
+
71
+ 「画像処理ソフトの入力データを作る」が目的であれば、言語はなんでもよいと思いますが、C++ のサンプルコードも貼っておきますね。
72
+
73
+ (C言語は面倒なので、省略します。すいません。)
74
+
75
+
76
+
77
+ ```cpp
78
+
79
+ #include <fstream>
80
+
81
+ #include <iostream>
82
+
83
+ #include <sstream>
84
+
85
+ #include <string>
86
+
87
+ #include <vector>
88
+
89
+
90
+
91
+ /**
92
+
93
+ * @brief 指定したパスからデータを読み込む。
94
+
95
+ * @param filepath ファイルパス
96
+
97
+ * @param data 読み込んだデータ
98
+
99
+ */
100
+
101
+ void readTxtData(
102
+
103
+ const std::string &filepath, std::vector<std::vector<double>> &data)
104
+
105
+ {
106
+
107
+ data.clear();
108
+
109
+ std::ifstream ifs(filepath);
110
+
111
+
112
+
113
+ std::string line;
114
+
115
+ while (std::getline(ifs, line)) {
116
+
117
+ if (line.empty())
118
+
119
+ continue;
120
+
121
+ std::vector<double> rowData;
122
+
123
+ std::stringstream lineStream(line);
124
+
125
+
126
+
127
+ double value;
128
+
129
+ while (lineStream >> value) {
130
+
131
+ rowData.push_back(value);
132
+
133
+ }
134
+
135
+
136
+
137
+ data.push_back(rowData);
138
+
139
+ }
140
+
141
+ }
142
+
143
+
144
+
145
+ /**
146
+
147
+ * @brief 指定したパスにデータを書き込む。
148
+
149
+ * @param filepath ファイルパス
150
+
151
+ * @param data 読み込んだデータ
152
+
153
+ */
154
+
155
+ void writeData(
156
+
157
+ const std::string &filepath, const std::vector<std::vector<double>> &data)
158
+
159
+ {
160
+
161
+ std::ofstream ofs(filepath, std::ios::out | std::ios::binary);
162
+
163
+ for (const auto &rowData : data) {
164
+
165
+ for (const auto &v : rowData)
166
+
167
+ ofs.write(reinterpret_cast<const char *>(&v), sizeof(v));
168
+
169
+ }
170
+
171
+ }
172
+
173
+
174
+
175
+ int main()
176
+
177
+ {
178
+
179
+ // データを読み込む。
180
+
181
+ std::vector<std::vector<double>> data;
182
+
183
+ readTxtData("data.csv", data);
184
+
185
+
186
+
187
+ // データを表示する。
188
+
189
+ for (const auto &rowData : data) {
190
+
191
+ for (const auto &v : rowData)
192
+
193
+ std::cout << v << " ";
194
+
195
+ std::cout << std::endl;
196
+
197
+ }
198
+
199
+
200
+
201
+ writeData("data.binary", data);
202
+
203
+ }
204
+
205
+ ```