Click here to Skip to main content
15,898,134 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Excel Page Pin
roadme27-Oct-06 8:42
roadme27-Oct-06 8:42 
GeneralRe: Excel Page Pin
roadme27-Oct-06 9:00
roadme27-Oct-06 9:00 
QuestionReading from File question... [modified] Pin
q_p25-Oct-06 11:20
q_p25-Oct-06 11:20 
AnswerRe: Reading from File question... Pin
led mike25-Oct-06 11:34
led mike25-Oct-06 11:34 
GeneralRe: Reading from File question... Pin
q_p25-Oct-06 11:40
q_p25-Oct-06 11:40 
GeneralRe: Reading from File question... Pin
Zac Howland25-Oct-06 11:47
Zac Howland25-Oct-06 11:47 
GeneralRe: Reading from File question... Pin
led mike25-Oct-06 11:49
led mike25-Oct-06 11:49 
AnswerRe: Reading from File question... Pin
Zac Howland25-Oct-06 11:45
Zac Howland25-Oct-06 11:45 
Here is the most flexible way:

#include <vector>
#include <algorothm>
#include <iterator>
#include <fstream>

class Student
{
	Student() : _StudentID(0) {}
	~Student() {}

	void setStudentID(long id) {_StudentID = id; }
	long getStudentID() const { return _StudentID; }

	std::vector<long>::iterator grades_begin() { return _Grades.begin(); }
	std::vector<long>::iterator grades_end() { return _Grades.end(); }

	void addGrade(long grade) {_Grades.push_back(grade); }
	void addGrades(const std::vector<long>& grades) { _Grades.insert(back_inserter(_Grades), grades.begin(), grades.end()); }
	unsigned long grade_count() const { return _Grades.size(); }
private:
	long _StudentID;
	std::vector<long> _Grades;
};

ostream& operator<<(ostream& os, const Student& s)
{
	os << s.getStudentID() << " ";
	copy(s.begin(), s.end(), ostream_iterator<long>(os, " "));
	os << endl;
	return os;
}

istream& operator>>(istream& is, Student& s)
{
	long id;
	std::vector<long> grades;
	is >> id;
	copy(istream_iterator<long>(is), istream_iterator<long>(), back_inserter(grades));
	s.setStudentID(id);
	s.addGrades(grades);
	return is;
}

int main()
{
	ifstream fin;
	fin.open("mydata.txt");
	std::vector<Student> students;
	copy(istream_iterator<Student>(fin), istream_iterator<Student>(), back_inserter(students));
	fin.close();
}


If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week

Zac

GeneralRe: Reading from File question... Pin
led mike25-Oct-06 12:01
led mike25-Oct-06 12:01 
GeneralRe: Reading from File question... Pin
Zac Howland25-Oct-06 12:53
Zac Howland25-Oct-06 12:53 
GeneralRe: Reading from File question... Pin
q_p25-Oct-06 12:16
q_p25-Oct-06 12:16 
GeneralRe: Reading from File question... Pin
Zac Howland25-Oct-06 12:55
Zac Howland25-Oct-06 12:55 
GeneralRe: Reading from File question... Pin
ThatsAlok25-Oct-06 20:57
ThatsAlok25-Oct-06 20:57 
GeneralRe: Reading from File question... Pin
Stephen Hewitt25-Oct-06 15:12
Stephen Hewitt25-Oct-06 15:12 
GeneralRe: Reading from File question... Pin
Zac Howland26-Oct-06 3:59
Zac Howland26-Oct-06 3:59 
GeneralRe: Reading from File question... Pin
ThatsAlok25-Oct-06 20:51
ThatsAlok25-Oct-06 20:51 
GeneralRe: Reading from File question... Pin
Zac Howland26-Oct-06 4:03
Zac Howland26-Oct-06 4:03 
QuestionRe: Reading from File question... Pin
David Crow26-Oct-06 4:34
David Crow26-Oct-06 4:34 
AnswerRe: Reading from File question... Pin
Zac Howland26-Oct-06 4:51
Zac Howland26-Oct-06 4:51 
GeneralRe: Reading from File question... Pin
David Crow26-Oct-06 5:02
David Crow26-Oct-06 5:02 
GeneralRe: Reading from File question... Pin
Zac Howland26-Oct-06 6:04
Zac Howland26-Oct-06 6:04 
QuestionRe: Reading from File question... Pin
David Crow29-May-07 10:12
David Crow29-May-07 10:12 
AnswerRe: Reading from File question... Pin
Zac Howland30-May-07 7:07
Zac Howland30-May-07 7:07 
GeneralRe: Reading from File question... Pin
David Crow30-May-07 7:22
David Crow30-May-07 7:22 
GeneralRe: Reading from File question... Pin
Zac Howland30-May-07 9:13
Zac Howland30-May-07 9:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.