int main() { InputStreamWrapper input("test.txt"); TextLineReader reader ( input.stream() ); string line ; ASSERT ( reader.next_line() ); line = reader.line_string() ; //first line - with explicit method to get the string ASSERT( line == "first line" ) ; ASSERT ( reader.next_line() ); line = reader ; //second line - with implicit conversion to std::string ASSERT( line == "second line" ) ; ASSERT ( reader.next_line() ); istream &is1 ( reader.line_stream() ); //third line - with explicit method to get the istream is1 >> line ; // read each word from the line ASSERT( line == "third" ) ; is1 >> line ; ASSERT ( line == "line" ) ; is1 >> line ; // read past the end of the line - this should fail (and not read the next word from the next line, as it would have happened with a normal istream; ASSERT ( ! is1 ) ; ASSERT ( reader.next_line() ); istream &is2 ( reader ) ; // fourth line - with implicit conversion to std::istream is2 >> line ; ASSERT( line == "fourth" ) ; is2 >> line ; ASSERT( line == "line" ) ; is1 >> line ; // read past the end of the line - this should fail (and not read the next word from the next line ASSERT ( !is2 ) ; //read past the end of the file - make sure it fails. ASSERT ( !reader.next_line() ) ; return 0; }
int main() { InputStreamWrapper input("test.txt"); TextLineReader reader ( input.stream() ); string line ; //Read first line ASSERT ( reader.next_line() ); line = reader; ASSERT( line == "first line" ) ; //Unget the current line - //The next call to 'reader.next_line' should return this SAME line //instead of actually reading the next line from the file. reader.unget_current_line(); //Read the next line - //but because of the 'unget' it should still return the first line ASSERT ( reader.next_line() ) ; line = reader ; ASSERT( line == "first line" ) ; ASSERT ( reader.line_number() == 1 ) ; //we should still be on line 1 //Read the next line - //This should be the real second line from the file. ASSERT ( reader.next_line() ) ; line = reader; ASSERT ( line == "second line" ); ASSERT ( reader.line_number() == 2 ) ; //Test multiple consecutive ungets for ( int i=0 ; i < 10; ++i ) { reader.unget_current_line(); ASSERT ( reader.next_line() ) ; line = reader; ASSERT ( line == "second line" ); ASSERT ( reader.line_number() == 2 ) ; } //Read the next line - //This should be the real third line from the file. ASSERT ( reader.next_line() ) ; line = reader; ASSERT ( line == "third line" ); ASSERT ( reader.line_number() == 3 ) ; //Unget a custom line string reader.unget_line("hello world"); //Read the next line - this should return the ungot text ("hello world") ASSERT ( reader.next_line() ) ; line = reader; ASSERT ( line == "hello world" ); return 0; }