Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- // using std::ifstream
- #include <iostream>
- // using std::cout, std::cerr
- #include <vector>
- // using std::vector
- #include <string>
- // using std::string, std::getline
- int main()
- {
- std::vector<std::string> keys;
- std::vector<int> values;
- const char* fname = "house_repairs.txt";
- std::ifstream input(fname);
- if(! input) {
- std::cerr << "unable to open " << fname << std::endl;
- return 1;
- }
- while(input) {
- /* first attempt to read an integer. If that fails, it must be a key */
- int value;
- if(input >> value)
- values.push_back(value);
- else if(input.fail()) {
- input.clear();
- std::string line;
- if(std::getline(input, line))
- keys.push_back(line);
- }
- if(input.bad()) {
- std::cerr << "IO error" << std::endl;
- return 1;
- }
- }
- for(std::vector<std::string>::iterator i = keys.begin(); i != keys.end();
- ++i)
- (std::cout << "Got key " << *i).put('\n');
- for(std::vector<int>::iterator i = values.begin(); i != values.end(); ++i)
- (std::cout << "Got value " << *i).put('\n');
- std::cout.flush();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement