How do you do that with a text file, Need to distinguish between integers and characters

Hi,
I am trying the following code but putback() apparently is returning a character and it destroy my number .
I thought about saving the current position in the text file before in >> c
and then use return back if not correct .
I am not sure how to do that .


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char c;
int n;
// read from text file
in >> c;
		if ((c >='a' && c <= 'z') || ((c >= 'A') && (c < 'Z')))
		{
			currentUser=c;
			cout << "executionTime is : " << currentUser <<endl;
			in >> childProcess;
			time1 = timePerUser/childProcess;
			idTemp=0;
		}
		else{
		in.putback (c);
		in >> n;
Using std::istream::peek() would be easier. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int next ;
while( ( next = istm.peek() ) != std::char_traits<char>::eof() )
{
    if( std::isdigit(next) )
    {
        int number ;
        istm >> number ;
        // use number
    }
    
    else
    {
        std::string str ;
        istm >> str ;
        // use str
    }
}

Hi,
Thanks for the reply , i tried that , and it didnt work.
This is what i did :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
c = in.peek();
		if (!( (c >= '0') && (c <= '9') ))
		{
			in >> c;
			currentUser=c;
			cout << "executionTime is : " << currentUser <<endl;
			in >> childProcess;
			time1 = timePerUser/childProcess;
			idTemp=0;
		}
		else{
	
		in >> n;
		info->readyTime=n;
never mind your solution works , i did a mistake somewhere
If the peek returned a white space, you need to throw it away.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int next ;

while( ( next = istm.peek() ) != std::char_traits<char>::eof() )
{
    if( std::isspace(next) ) istm.get() ;

    else if( std::isdigit(next) )
    {
        int number ;
        istm >> number ;
        std::cout << "number: " << number << '\n' ;
    }

    else
    {
        std::string str ;
        istm >> str ;
        std::cout << "string: " << str << '\n' ;
    }
}
Topic archived. No new replies allowed.