Read integers from file with text in C++

Hello,

I want to read the integers from a file with numbers and letters. For example the file is file.txt:
kdgfg8aa
907dffd99ss
kaasjelgg65
9h6hb0v32


Then I want to display which of the numbers are odd or even.
So, I want my output:
8 is an even number
907 is an odd number.
99 is an odd number.
65 is an odd number.
9 is an odd number.
6 is an even number.
0 is an even number.
32 is an even number.


Is it possible to do this without using strings or vectors?
Last edited on
Yes. You could use C-style char arrays. Or you could read character by character, and construct any numbers as you read them.
I guess it depends how big a number you are prepared to tolerate. @dutch's solution (below) will certainly be much faster if the number fits in an int.

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
#include <iostream>
#include <sstream>
#include <cctype>
using namespace std;

void filter( int n ){ cout << " is an " << ( n % 2 ? "odd" : "even" ) << " number\n"; }


void extract( istream &in )
{
   bool state = false;
   char c, last;
   while( in.get( c ) )
   {
      if ( isdigit( c ) )
      {
         cout << c;
         last = c;
         state = true;
      }
      else
      {
         if ( state ) filter( last - '0' );
         state = false;
      }
   }
   if ( state ) filter( last - '0' );
}


int main()
{
   stringstream in( "kdgfg8aa   \n"
                    "907dffd99ss\n"
                    "kaasjelgg65\n"
                    "9h6hb0v32  \n" );
   extract( in );
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;

int main() {
    ifstream fin("filename");
    char ch;
    while (fin.get(ch)) {
        if (isdigit(ch)) {
            fin.unget();
            int n;
            fin >> n;
            cout << n << '\n';
        }
    }
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cctype>
#include <fstream>

int main()
{
	const char* state[] = {"even", "odd"};

	std::ifstream fin("filename.txt");

	if (!fin.is_open())
		return (std::cout << "Cannot open input file\n"), 1;

	for (int ch, num; (ch = fin.peek()) != EOF; )
		if (std::isdigit(ch)) {
			fin >> num;
			std::cout << num << " is an " << state[num % 2] << " number.\n";
		} else
			fin.ignore();
}




8 is an even number.
907 is an odd number.
99 is an odd number.
65 is an odd number.
9 is an odd number.
6 is an even number.
0 is an even number.
32 is an even number.
Last edited on
Note that unget() is not guaranteed to succeed and if used the stream state should be checked. unget() uses sungetc() for the underlying stream buffer which fails if a putback position is not available.
seeplus seems to be implying that there may be something wrong with my use of unget. This evaluation clearly does not apply to the way I used it.
Topic archived. No new replies allowed.