Click here to Skip to main content
15,949,686 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questioninputing string & int from file to array. Pin
Ramper30-Oct-06 9:43
Ramper30-Oct-06 9:43 
QuestionRe: inputing string & int from file to array. Pin
David Crow30-Oct-06 9:47
David Crow30-Oct-06 9:47 
AnswerRe: inputing string & int from file to array. Pin
Ramper30-Oct-06 9:52
Ramper30-Oct-06 9:52 
GeneralRe: inputing string & int from file to array. Pin
Zac Howland30-Oct-06 10:45
Zac Howland30-Oct-06 10:45 
QuestionRe: inputing string & int from file to array. Pin
David Crow30-May-07 9:24
David Crow30-May-07 9:24 
AnswerRe: inputing string & int from file to array. Pin
Zac Howland31-May-07 5:43
Zac Howland31-May-07 5:43 
GeneralRe: inputing string & int from file to array. Pin
David Crow31-May-07 6:00
David Crow31-May-07 6:00 
GeneralRe: inputing string & int from file to array. Pin
Zac Howland31-May-07 7:44
Zac Howland31-May-07 7:44 
Here is a working example with the data format you gave:

#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

class Student
{
public:
	Student() : _Name("") {}
	~Student() {}

	void setName(const string& name) { _Name = name; }
	string getName() const { return _Name; }

	void setGrades(const vector<int>& grades) { _Grades.assign(grades.begin(), grades.end()); }
	vector<int> getGrades() const { return _Grades; }
private:
	string _Name;
	vector<int> _Grades;
};

std::ostream& operator<<(std::ostream& os, const Student& s)
{
	os << s.getName() << " ";	// NOTE:  older versions of STL will require a character buffer instead
	const vector<int> grades = s.getGrades();
	copy(grades.begin(), grades.end(), ostream_iterator<int>(os, " "));
	os << std::endl;
	return os;
}

std::istream& operator>>(std::istream& is, Student& s)
{
	string name = "";	// NOTE:  older versions of STL will require a character buffer instead
	is >> name;
	string sGrades = "";
	getline(is, sGrades);
	vector<int> grades;
	stringstream ss(sGrades);
	copy(istream_iterator<int>(ss), istream_iterator<int>(), back_inserter(grades));
	s.setName(name);
	s.setGrades(grades);
	return is;
}

int main()
{
	ifstream fin;
	vector<Student> students;
	fin.open("data.txt");
	copy(istream_iterator<Student>(fin), istream_iterator<Student>(), back_inserter(students));
	fin.close();
	// do whatever you want with students vector


	std::copy(students.begin(), students.end(), ostream_iterator<Student>(cout, "\n\n"));
}




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

QuestionRe: inputing string &amp; int from file to array. Pin
David Crow31-May-07 8:42
David Crow31-May-07 8:42 
AnswerRe: inputing string &amp; int from file to array. Pin
Zac Howland31-May-07 9:07
Zac Howland31-May-07 9:07 
GeneralRe: inputing string &amp; int from file to array. Pin
David Crow1-Jun-07 4:13
David Crow1-Jun-07 4:13 
QuestionCPrintDialog issue Pin
Andy H30-Oct-06 8:59
Andy H30-Oct-06 8:59 
QuestionVS 2003 .NET System Path Pin
BlitzPackage30-Oct-06 8:20
BlitzPackage30-Oct-06 8:20 
AnswerRe: VS 2003 .NET System Path Pin
Mark Salsbery30-Oct-06 9:01
Mark Salsbery30-Oct-06 9:01 
GeneralRe: VS 2003 .NET System Path Pin
BlitzPackage30-Oct-06 9:11
BlitzPackage30-Oct-06 9:11 
GeneralRe: VS 2003 .NET System Path Pin
Mark Salsbery30-Oct-06 9:37
Mark Salsbery30-Oct-06 9:37 
GeneralRe: VS 2003 .NET System Path Pin
BlitzPackage30-Oct-06 10:16
BlitzPackage30-Oct-06 10:16 
QuestionON_MESSAGE event Pin
edvintas30-Oct-06 6:33
edvintas30-Oct-06 6:33 
AnswerRe: ON_MESSAGE event Pin
Mark Salsbery30-Oct-06 6:37
Mark Salsbery30-Oct-06 6:37 
GeneralRe: ON_MESSAGE event Pin
edvintas30-Oct-06 6:41
edvintas30-Oct-06 6:41 
GeneralRe: ON_MESSAGE event Pin
Mark Salsbery30-Oct-06 6:47
Mark Salsbery30-Oct-06 6:47 
GeneralRe: ON_MESSAGE event Pin
edvintas30-Oct-06 7:02
edvintas30-Oct-06 7:02 
AnswerRe: ON_MESSAGE event Pin
Maximilien30-Oct-06 6:56
Maximilien30-Oct-06 6:56 
GeneralRe: ON_MESSAGE event Pin
edvintas30-Oct-06 7:06
edvintas30-Oct-06 7:06 
AnswerRe: ON_MESSAGE event Pin
S Douglas30-Oct-06 21:28
professionalS Douglas30-Oct-06 21:28 

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.