1行に1レコードがあるので、std::getline
はあなたの友人になります(std::string
)。
はのが何かを試してみましょう:
std::string record_text;
std::getline(my_data_file, record_text);
我々は数にテキストレコードを変えるのを助けるためにstd::istringstream
を使用することができます。
std::istringstream record_stream(record_text);
std::vector<int> student_values;
int student_id;
record_stream >> student_id;
int value;
while (record_stream >> value)
{
student_values.push_back(value);
}
私は学生の値を含むようにstd::vector
を使用しています。別のコンテナを使用する必要があります。
編集1:オーバーロードの抽出演算子
あなたはインストラクターと仲間の学生を感動したい場合は、struct
のレコードをモデル化し、入力演算子オーバーロードする必要があります。
struct Student_Record
{
int id;
std::vector<int> values;
friend std::istream& operator>>(istream& input, Student_Record& sr);
};
std::istream& operator>>(istream& input, Student_Record& sr)
{
// See above on how to read a line of data.
// Be sure to use "sr." when accessing the structure variables,
// such as sr.id
return input;
}
オーバーロードされたオペレータができますがあなたは簡単な入力を持っています:
std::vector<Student_Record> database;
Student_Record sr;
while (my_data_file >> sr)
{
database.push_back(sr);
}
教師と仲間にはStackOverflowを必ずお勧めしますへこみ。
コードフォームでファイルをコメントしてください。 – blazerix
"コードグリッチ"にならないでください - これまでに試したことを教えてください –
"stackoverflow C++ファイル構造体を読み取る " –