質問編集履歴

4

int32_t様のコード更新

2022/02/25 06:02

投稿

roatt
roatt

スコア45

test CHANGED
File without changes
test CHANGED
@@ -143,10 +143,10 @@
143
143
  return token; //セルの値を返す
144
144
  }
145
145
  else {
146
- return nullopt;
147
146
  }
148
147
  ++cur_col; //次のセルへ
149
148
  }
149
+ return nullopt;
150
150
  }
151
151
 
152
152
  /**

3

int32_t様コード変更

2022/02/25 04:54

投稿

roatt
roatt

スコア45

test CHANGED
File without changes
test CHANGED
@@ -133,24 +133,19 @@
133
133
  *@return csvセルの値
134
134
  */
135
135
  optional<string> get_cell(string line, int col) {
136
- try {
136
+
137
- string token; //分割したセルの値を格納する変数
137
+ string token; //分割したセルの値を格納する変数
138
- int cur_col = 0; //処理中の列の列番号
138
+ int cur_col = 0; //処理中の列の列番号
139
- istringstream stream(line); //行の文字列をカンマで分割
139
+ istringstream stream(line); //行の文字列をカンマで分割
140
-
140
+
141
- while (getline(stream, token, ',')) {
141
+ while (getline(stream, token, ',')) {
142
- if (cur_col == col) { //指定されたセルが見つかった
142
+ if (cur_col == col) { //指定されたセルが見つかった
143
- return token; //セルの値を返す
143
+ return token; //セルの値を返す
144
- }
144
+ }
145
- else {
145
+ else {
146
- return nullopt;
146
+ return nullopt;
147
- }
147
+ }
148
- ++cur_col; //次のセルへ
148
+ ++cur_col; //次のセルへ
149
- }
150
- throw "指定されたセルが見つかりませんでした"; //指定されたセルが見つからないときは例外を投げる
151
- }
152
- catch (char const* message) {
153
-
154
149
  }
155
150
  }
156
151
 
@@ -160,7 +155,7 @@
160
155
  *@param row 行番号
161
156
  *@aram col_name 列名
162
157
  */
163
- [[nodiscard]] vector<string> get_data(int row) {
158
+ [[nodiscard]] vector<string> get_data() {
164
159
  vector<string> cellData;
165
160
  string filename("C:/Users/AIM-CAE-PC12/Documents/Unreal Projects/Arduino_test/csvSearch_test.csv");
166
161
  ifstream fin(filename); //ファイルを開く
@@ -171,11 +166,11 @@
171
166
  string line; //ファイルの行を格納する変数
172
167
 
173
168
  while (getline(fin, line)) { //ファイルを1行ずつ読み込む
174
- if (cur_row == row) { //指定された行が見つかった
169
+
175
- if (optional<string> cell = get_cell(line, col)) {
170
+ if (optional<string> cell = get_cell(line, col)) {
176
- cellData.push_back(*cell);
171
+ cellData.push_back(*cell);
177
- }
172
+ }
178
- }
173
+
179
174
  ++cur_row;
180
175
  }
181
176
  return cellData; //行の文字列からセルの値を取得
@@ -184,15 +179,8 @@
184
179
  int main()
185
180
  {
186
181
  vector<string> csvData;
187
- int i = 2;
182
+
188
-
189
- csvData = get_data(i);
183
+ csvData = get_data();
190
-
191
- /*do {
192
- csvData = (get_data(i));
193
-
194
- i++;
195
- } while (csvData.size() != 0);*/
196
184
 
197
185
  for (size_t z = 0; z < csvData.size(); ++z) {
198
186
  cout << csvData[z] << endl; //値が入っているか確認用

2

コード変更

2022/02/25 01:46

投稿

roatt
roatt

スコア45

test CHANGED
File without changes
test CHANGED
@@ -134,19 +134,19 @@
134
134
  */
135
135
  optional<string> get_cell(string line, int col) {
136
136
  try {
137
- optional<string> token; //分割したセルの値を格納する変数
137
+ string token; //分割したセルの値を格納する変数
138
- int cur_col = 0; //処理中の列の列番号
138
+ int cur_col = 0; //処理中の列の列番号
139
- istringstream stream(line); //行の文字列をカンマで分割
139
+ istringstream stream(line); //行の文字列をカンマで分割
140
-
140
+
141
- while (getline(stream, token, ',')) { //オーバーロードされた関数"getline"のインスタンスが引数リストと一致しません ★
141
+ while (getline(stream, token, ',')) {
142
- if (cur_col == col) { //指定されたセルが見つかった
142
+ if (cur_col == col) { //指定されたセルが見つかった
143
- return token; //セルの値を返す
143
+ return token; //セルの値を返す
144
- }
144
+ }
145
- else {
145
+ else {
146
- return nullopt;
146
+ return nullopt;
147
- }
147
+ }
148
- ++cur_col; //次のセルへ
148
+ ++cur_col; //次のセルへ
149
- }
149
+ }
150
150
  throw "指定されたセルが見つかりませんでした"; //指定されたセルが見つからないときは例外を投げる
151
151
  }
152
152
  catch (char const* message) {
@@ -172,7 +172,9 @@
172
172
 
173
173
  while (getline(fin, line)) { //ファイルを1行ずつ読み込む
174
174
  if (cur_row == row) { //指定された行が見つかった
175
- optional<string> cell = get_cell(line, col);
175
+ if (optional<string> cell = get_cell(line, col)) {
176
+ cellData.push_back(*cell);
177
+ }
176
178
  }
177
179
  ++cur_row;
178
180
  }
@@ -184,15 +186,17 @@
184
186
  vector<string> csvData;
185
187
  int i = 2;
186
188
 
189
+ csvData = get_data(i);
190
+
187
- do {
191
+ /*do {
188
192
  csvData = (get_data(i));
189
193
 
190
194
  i++;
191
- } while (csvData.size() != 0);
195
+ } while (csvData.size() != 0);*/
192
196
 
193
197
  for (size_t z = 0; z < csvData.size(); ++z) {
194
198
  cout << csvData[z] << endl; //値が入っているか確認用
195
- }
199
+ }
196
200
  }
197
201
  ```
198
202
  ### 試したこと

1

int32_t様ご助力のコードを挿入

2022/02/24 13:26

投稿

roatt
roatt

スコア45

test CHANGED
File without changes
test CHANGED
@@ -109,7 +109,92 @@
109
109
  }
110
110
  }
111
111
  ```
112
-
112
+ ↓int32_t様ご助力のコード
113
+ ```cpp
114
+ #include <iostream>
115
+ #include <fstream>
116
+ #include <map>
117
+ #include <sstream>
118
+ #include <string>
119
+ #include <vector>
120
+ #include <optional>
121
+
122
+ using std::cout; using std::endl;
123
+ using std::ifstream; using std::string;
124
+ using std::getline; using std::istringstream;
125
+ using std::vector;
126
+ using std::optional;
127
+ using std::nullopt;
128
+
129
+ /**
130
+ *csvファイルの行の文字列から指定された列のセルの値を取得する
131
+ *@param line csvファイルの行の文字列
132
+ *@param col 列番号
133
+ *@return csvセルの値
134
+ */
135
+ optional<string> get_cell(string line, int col) {
136
+ try {
137
+ optional<string> token; //分割したセルの値を格納する変数
138
+ int cur_col = 0; //処理中の列の列番号
139
+ istringstream stream(line); //行の文字列をカンマで分割
140
+
141
+ while (getline(stream, token, ',')) { //オーバーロードされた関数"getline"のインスタンスが引数リストと一致しません ★
142
+ if (cur_col == col) { //指定されたセルが見つかった
143
+ return token; //セルの値を返す
144
+ }
145
+ else {
146
+ return nullopt;
147
+ }
148
+ ++cur_col; //次のセルへ
149
+ }
150
+ throw "指定されたセルが見つかりませんでした"; //指定されたセルが見つからないときは例外を投げる
151
+ }
152
+ catch (char const* message) {
153
+
154
+ }
155
+ }
156
+
157
+ /**
158
+ *csvファイルから指定したセルの値を取得する
159
+ *@param filename csvファイルのファイル名
160
+ *@param row 行番号
161
+ *@aram col_name 列名
162
+ */
163
+ [[nodiscard]] vector<string> get_data(int row) {
164
+ vector<string> cellData;
165
+ string filename("C:/Users/AIM-CAE-PC12/Documents/Unreal Projects/Arduino_test/csvSearch_test.csv");
166
+ ifstream fin(filename); //ファイルを開く
167
+ int col = 2; //列名に"値R"の列番号を取得
168
+ //string line = get_row(fin, row); //csvファイルの指定された行の文字列を取得
169
+
170
+ int cur_row = 1; //読み込んだ行数。先頭行は読み込み済みだから「1」を初期値に指定
171
+ string line; //ファイルの行を格納する変数
172
+
173
+ while (getline(fin, line)) { //ファイルを1行ずつ読み込む
174
+ if (cur_row == row) { //指定された行が見つかった
175
+ optional<string> cell = get_cell(line, col);
176
+ }
177
+ ++cur_row;
178
+ }
179
+ return cellData; //行の文字列からセルの値を取得
180
+ }
181
+
182
+ int main()
183
+ {
184
+ vector<string> csvData;
185
+ int i = 2;
186
+
187
+ do {
188
+ csvData = (get_data(i));
189
+
190
+ i++;
191
+ } while (csvData.size() != 0);
192
+
193
+ for (size_t z = 0; z < csvData.size(); ++z) {
194
+ cout << csvData[z] << endl; //値が入っているか確認用
195
+ }
196
+ }
197
+ ```
113
198
  ### 試したこと
114
199
  ①get_data()、get_cell()をstring<vector>に、get_cell()内のstring tokenもstring<vector>に変更し、データ型を合わせようとしましたが、get_cell()内のgetline(stream, token, ',')で以下の★エラー文と int main内のwhile (get_data(i) == "")で △エラー文がでてしまいます。
115
200
  ②string → vector<string>へ型変換できないかも調べましたが、発見することができませんでした。