How do I skip over columns when reading from text file

Hi. How do i skip certain numbers when im reading from text file


I'm only supposed to read the name of the state(MAINE) and read admission number(23 in this case). and output them

so far i got this. this gives me correct result for the first line and gives me 49 lines of trash.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

inpute 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


  	while (n < 50)
		{
			GetString(fin, names[n], 14, '\n');
			fin >> num1[n];
			fin >> num2[n];
			fin >> num3[n];
			fin >> admission[n];
			fin >> num4[n];
			fin >> num5[n];
			fin >> num6[n]; 
			n++;
		}
		for (n = 0; n < 50; n++)
		{
			cout << names[n] << endl;
			cout << admission[n] << endl;
		}
Last edited on
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

and this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace 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.



Thank you for replying. Actually the input file is set up nicely and everything is in line.

state names are left justified from column 1-14.
from column 15-36 is unwanted items
read column 37
and anything beyond column 38 is also trash


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
Massachusetts   5767037   8257 1788  6 Boston         MA 02800
Rhode Island     947154   1214 1790 13 Providence     RI 03000
Connecticut     3107576   5009 1788  5 Hartford       CT 07000


this is how it looks
Last edited on
Thanks. That makes sense. It's getting (very) late now in my part of the world, so I'm not able to respond further right now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

struct admission_info { std::string state ; int admission_number ; };

std::istream& get_info( std::istream& stm, admission_info& info )
{
    info = {} ;

    std::string line ;
    if( std::getline( stm, line ) )
    {
        try
        {
            // state names are left justified from column 1-14
            std::string state = line.substr( 0, 14 ) ;
            // trim spaces on the right
            while( !state.empty() && std::isspace( state.back() ) ) state.pop_back() ;

            // from column 15-36 is unwanted items read column 37
            info = { state, std::stoi( line.substr(36) ) } ;
        }

        catch( const std::exception& ) // badly formed line
        {
            stm.clear( std::ios::failbit ) ;
        }
    }

    return stm ;
}

int main()
{
    const std::string file_name = "test.txt" ;

    // create a test file
    std::ofstream(file_name) <<
                "Maine           1124660  33215 1820 23 Augusta        ME 05000\n"
                "New Hampshire    920610   9304 1788  9 Concord        NH 03900\n"
                "Vermont          511456   9609 1791 14 Montpelier     VT 06000\n"
                "Massachusetts   5767037   8257 1788  6 Boston         MA 02800\n"
                "Rhode Island     947154   1214 1790 13 Providence     RI 03000\n"
                "Connecticut     3107576   5009 1788  5 Hartford       CT 07000\n" ;

    std::ifstream file(file_name) ;
    admission_info info ;
    while( get_info( file, info ) )
            std::cout << std::quoted(info.state) << "  " << info.admission_number << '\n' ;
}

http://coliru.stacked-crooked.com/a/7b55eec1fef25fc5
One more response. This may be useful simply because it uses a completely different approach to the earlier reply.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <fstream>

using namespace 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.
Last edited on
Thanks guys. Helped me alot
Last edited on
closed account (48T7M4Gy)
I've never used find_last_not_of before ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
#include <string>

int main ()
{
    std::string line;
    std::string state;
    int number;
    
    // *** http://www.cplusplus.com/reference/string/basic_string/find_last_not_of/
    std::string whitespaces (" \t\f\v\n\r");
    size_t pos = 0;
    
    std::ifstream myfile ("states.txt");
    if (myfile.is_open())
    {
        
        while(getline(myfile, line))
        {
            state = line.substr(0, 14);
            pos = state.find_last_not_of(whitespaces); // ***
            state = state.substr(0, pos + 1);
            
            number = stoi(line.substr(36,2));
            
            std::cout << state << ", " << number << '\n';
        }
        
        myfile.close();
    }
    
    else
        std::cout << "Unable to open file";
    
    return 0;
}
Maine, 23
New Hampshire, 9
Vermont, 14
Massachusetts, 6
Rhode Island, 13
Connecticut, 5
Program ended with exit code: 0
states.txt:
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
Massachusetts   5767037   8257 1788  6 Boston         MA 02800
Rhode Island     947154   1214 1790 13 Providence     RI 03000
Connecticut     3107576   5009 1788  5 Hartford       CT 07000
Last edited on
Topic archived. No new replies allowed.