私が取り組んでいる課題については、ファイルをストリームして2点間の距離を見つけることになっています。 ifileには、1行に6つの整数があります。各ポイントの距離は3ポイントであるので、私は3のサイズの2つのアレイを作ることにしました。しかし、私は配列にデータを得るのが非常に困難です。以下は、ifileの外観のサンプルです。Fstreamで問題が発生し、データを配列に格納する
2 5 2 8 5 2
9 0 0 1 3 0
0 8 8 9 6 3
3 9 1 8 5 2
1 4 10 0 0 9
9 8 1 3 6 8
9 10 7 3 2 5
ここには、私が取り組んでいるコードのサンプルがあります。
#include <cmath>
#include <fstream>
#include <string>
#include <iostream>
using std::cout;
using std::endl;
using std::sqrt;
using std::ifstream;
using std::string;
namespace roz
{
bool readpoints(ifstream&ifile, int p1[], int p2[], int sz);
static int
pointdistance()
{
const int coords = 3;
int p1[coords];
int p2[coords];
ifstream ifile;
string path = "C:\\Users\\Joshbox\\Desktop\\points-3d.txt";
ifile.open(path);
if (!ifile) {
cout << "File not found: " << path << endl;
return -1;
}
while (readpoints(ifile, p1, p2, coords)) {
}
return 0;
}
bool readpoints(ifstream&ifile, int p1[], int p2[], int sz) {
string data;
while (getline(ifile, data)) {
for (int a = 1; a < sz; a++) {
ifile >> p1[a];
}
cout << p1;
}
return true;
}
static int(*currentAssignment)() = pointdistance;
int cse()
{
return currentAssignment();
}
}
わかりました...だから何ですあなたは持っている問題? – cdhowie
@cdhowieよく私のポイントの1つを試してみると、私は奇妙な出力を得る:F8CC0093。私は過去2日間に自分のコードを使って遊んでいましたが、なぜこれが起きているのかわかりません。 – jrooz
'getline(ifile、data))'は文字列に行を得ます。必要なものを持っていますが、 'ifile >> p1 [a];を実行します。 ' - 代わりに' data'を使います。たぶん 'cout <<データ<< '\ n';'ちょうどあなたが読んだことを見て、それを解析する小さな関数を書いてください – doctorlove