overwriting << operator to read a text file.

I have to overwrite the operator>>. The conditions is to take in an numbers and store it into an array until the file contains a ';' from a .txt file.
file ex.
1018929238387; 29323867;
387374848938948747
3837474838734;

I am completely lost and really need help. I have never worked with .txt files and this is my first time overriding the >> operator. I do not know how to work with whitespaces and endlines to skip them from a file.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::istream& operator>>(std::istream& in, bigint array){

  bool semi = false ;
  char ch;
  while(!semi){
    while(in.get(ch)){
      if(in.get(ch)==';'){
        semi=true ;
        break ;
      }
    }
  }
  return in ;
}

Couldn't you just read a character, then do a simple check like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
switch(ch) {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
        bigint_data_array.add(ch); // or whatever you need to do to add to the array
        break;
    case ';':
        semi = true;
        break;
    default:
        // ignore
        break;
}
Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
char ch ;
while( in.get(ch) ) // get the next character 
{
    // if it is a decimal digit, add the digit to the array
    if( std::isdigit(ch) )
    {
         int digit = ch - '0' ;
        // add digit to array
    }

    // if it is a ; or a white space, throw it away and end the loop
    else if( ch == ';' || std::isspace(ch) ) break ;

    else // if it is any other character, there is an arror
    {
         in.putback(ch) ; // put the character back into the input stream
         in.setstate( std::ios_base::failbit ) ; // put the stream in a failed state
         break ; // and end the loop
    }
}

Last edited on
Topic archived. No new replies allowed.