I'm not sure there's an easy way to do this with your file layout.
How is the end of the state name to be identified? is it a fixed-width field (specific number of characters) or is there a delimiter to mark the end of the name?
So far I tried the following input file:
Maine 1124660 33215 1820 23 Augusta ME 05000
New Hampshire 920610 9304 1788 9 Concord NH 03900
Vermont 511456 9609 1791 14 Montpelier VT 06000
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
usingnamespace std;
int main()
{
ifstream fin("data.txt");
string state; // name of the state
int num; // admission number
string dummy; // generic field for something
string line;
while ( getline(fin, line) )
{
istringstream ss(line);
if (ss >> state >> dummy >> dummy >> dummy >> num)
cout << "State: " << state << " number " << num << '\n';
}
}
which results in this output:
State: Maine number 23
State: New number 1788
State: Vermont number 14
So far, my attempt fails with New Hampshire because the name contains a space.
It would be possible to distinguish between numeric and alphabetic data, if that is really needed. I didn't try that yet.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
int main()
{
ifstream fin("data.txt");
string line;
while ( getline(fin, line) )
{
if (line.size() > 37)
{
string state = line.substr(0, 14); // name of the state
string number = line.substr(36, 2); // admission number
cout << state << " " << number << '\n';
}
}
}
Maine 23
New Hampshire 9
Vermont 14
Massachusetts 6
Rhode Island 13
Connecticut 5
Edit: thanks to JLBorges for a somewhat more thorough response.