Skip to content

Instantly share code, notes, and snippets.

@Rhomboid
Created October 31, 2012 19:53
Show Gist options
  • Save Rhomboid/3989402 to your computer and use it in GitHub Desktop.
Save Rhomboid/3989402 to your computer and use it in GitHub Desktop.
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <stdexcept>
class studentfile {
public:
studentfile(const std::string &filename);
void print_out(std::ostream &where);
private:
struct student {
std::string name;
double gpa;
long id;
char gender;
student() : name(""), gpa(0), id(0), gender('?') {};
student(std::istringstream &n) : name(n.str()), gpa(0), id(0), gender('?') {};
};
std::vector<student> students;
};
static bool case_insensitive_equals(const std::string &lhs, const std::string &rhs)
{
if(lhs.size() != rhs.size())
return false;
for(size_t i = 0; i < lhs.size(); i++)
if(std::tolower(lhs[i]) != std::tolower(rhs[i]))
return false;
return true;
}
studentfile::studentfile(const std::string &filename)
{
std::ifstream in(filename.c_str());
if(!in)
throw std::runtime_error("Can't open file");
student cur_student;
std::string line;
while(getline(in, line)) {
size_t lhs_begin = line.find_first_not_of(" \t");
if(lhs_begin == std::string::npos || line[lhs_begin] == '#')
continue;
size_t lhs_end = line.find_first_of("= \t", lhs_begin);
if(lhs_end == std::string::npos)
continue;
size_t rhs_begin = line.find_first_not_of("= \t", lhs_end);
if(rhs_begin == std::string::npos)
continue;
std::string lhs(line.substr(lhs_begin, lhs_end - lhs_begin));
std::istringstream rhs(line.substr(rhs_begin, std::string::npos));
if(case_insensitive_equals(lhs, "name")) {
if(cur_student.name.size())
students.push_back(cur_student);
cur_student = student(rhs);
} else if(case_insensitive_equals(lhs, "gpa"))
rhs >> cur_student.gpa;
else if(case_insensitive_equals(lhs, "id"))
rhs >> cur_student.id;
else if(case_insensitive_equals(lhs, "gender"))
rhs.get(cur_student.gender);
}
if(cur_student.name.size())
students.push_back(cur_student);
}
void studentfile::print_out(std::ostream &where)
{
for(std::vector<student>::iterator i = students.begin(); i != students.end(); i++)
where << "name: " << i->name << ", id: " << i->id << ", gender: " << i->gender << ", gpa: " << i->gpa << std::endl;
}
int main()
{
studentfile sf("20121031.txt");
sf.print_out(std::cout);
}
name = Rick James
ID= 123456
GPA =9.2
gender=M
# mixed items
name = Hermione Granger
GPA = 11.2
gender = f
ID = 123457
# mixed, missing, and extra fields
name = Henry Ramirez
GPA = 12.3
ID = 111888
major = ChE
class = soph
ID = 788531
# missing fields
name=Suzie Shah
geNDEr=t
$ g++ -Wall -Wextra -pedantic -O2 -g -std=c++98 20121031.cpp -o 20121031
$ ./20121031
name: Rick James, id: 123456, gender: M, gpa: 9.2
name: Hermione Granger, id: 123457, gender: f, gpa: 11.2
name: Henry Ramirez, id: 788531, gender: ?, gpa: 12.3
name: Suzie Shah, id: 0, gender: t, gpa: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment