Ex5: Input Streams
This commit is contained in:
		
							parent
							
								
									be584af6c3
								
							
						
					
					
						commit
						a54f03b178
					
				
					 3 changed files with 130 additions and 0 deletions
				
			
		
							
								
								
									
										43
									
								
								ex5.1/readfromstdin.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								ex5.1/readfromstdin.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,43 @@ | |||
| #include <iostream> | ||||
| #include <iomanip> | ||||
| 
 | ||||
| int main() { | ||||
| 	int input = 0; | ||||
| 
 | ||||
| 	std::cout << "Give me an Integer in hex: "; | ||||
| 
 | ||||
| 	std::cin >> std::hex >> input ; | ||||
| 
 | ||||
| 	std::cout << "Dec: " << input << std::endl; | ||||
| 
 | ||||
| 	std::cout << "Hex: " ; | ||||
| 	std::cout << std::hex << input << std::endl; | ||||
| 
 | ||||
| 	std::cout << "Octal: " ; | ||||
| 	std::cout << std::oct << input << std::endl; | ||||
| 
 | ||||
| 
 | ||||
| 	float f1, f2, f3 ; | ||||
| 	std::cout << "Give me three floats: " << std::endl; | ||||
| 	std::cout << "f1: " ; | ||||
| 	std::cin >> f1 ; | ||||
| 
 | ||||
| 	std::cout << "f2: " ; | ||||
| 	std::cin >> f2 ; | ||||
| 
 | ||||
| 	std::cout << "f3: " ; | ||||
| 	std::cin >> f3 ; | ||||
| 
 | ||||
| 	std::cout << "The following floats were given: " << std::endl; | ||||
| 	std::cout << std::scientific << f1 << " " << f2 << " " << f3 << std::endl; | ||||
| 
 | ||||
| 
 | ||||
| 	std::cout << "That is  20 chars columns per float: " << std::endl; | ||||
| 	std::cout << std::setw(20) << "ValueA" << " " << std::setw(20) << "ValueB" << " "<< std::setw(20) << "ValueC" << std::endl; | ||||
| 	std::cout << std::setfill('-') << std::setw(3*20 + 2 + 1) << " " << std::endl ; | ||||
| 	std::cout << std::setfill(' ') << std::setw(20) << std::scientific << f1 << " " << std::setw(20) << f2 << " "<< std::setw(20) << f3 << std::endl; | ||||
| 
 | ||||
| 
 | ||||
| 	std::cout << "With 3 digit precision: " << std::endl; | ||||
| 	std::cout << std::left << std::setprecision(3) << std::scientific << f1 << " " << f2 << " " << f3 << std::endl; | ||||
| } | ||||
							
								
								
									
										35
									
								
								ex5.2/main.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								ex5.2/main.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,35 @@ | |||
| #include <fstream> | ||||
| #include <iostream> | ||||
| 
 | ||||
| 
 | ||||
| int main() { | ||||
| 	 | ||||
| 	std::ifstream fh("data1.txt"); | ||||
| 
 | ||||
| 	if ( fh ) { | ||||
| 		std::cout <<  "File opened OK" << std::endl; | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		std::cout << "Opening File Failed" << std::endl; | ||||
| 		return 1; | ||||
| 	} | ||||
| 
 | ||||
| 	int number ; | ||||
| 
 | ||||
| 	while(fh >> number) { | ||||
| 		if ( number == 0 ) { | ||||
| 			std::cout << "Found a Zero: Breaking Loop" << std::endl; | ||||
| 			break; | ||||
| 		} | ||||
| 
 | ||||
| 		std::cout << "Found number: " << number << std::endl; | ||||
| 
 | ||||
| 		if ( fh.eof() ) { | ||||
| 			std::cout << "EOF" << std::endl; | ||||
| 			break; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
							
								
								
									
										52
									
								
								ex5.3/main.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								ex5.3/main.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,52 @@ | |||
| #include <fstream> | ||||
| #include <iostream> | ||||
| #include <cstring> | ||||
| #include <sstream> | ||||
| 
 | ||||
| int main( int argc, char* argv[] ) { | ||||
| 
 | ||||
| 
 | ||||
| 	if ( argc != 2) { | ||||
| 		std::cout << "usage: " << argv[0] << " FILENAME" << std::endl; | ||||
| 		return 1; | ||||
| 	} | ||||
| 
 | ||||
| 	std::ifstream fh(argv[1]); | ||||
| 
 | ||||
| 	if ( !fh ) { | ||||
| 		std::cout << "Error opening File '" << argv[1] << "'" << std::endl; | ||||
| 		return 2; | ||||
| 	} | ||||
| 
 | ||||
| 	unsigned int linecount = 0; | ||||
| 	unsigned int charcount = 0; | ||||
| 	unsigned int wordcount = 0; | ||||
| 	const int bufsize = 100; | ||||
| 	char buf[bufsize]; | ||||
| 
 | ||||
| 	const int linebufsize = 10; | ||||
| 	char linebuf[linebufsize]; | ||||
| 
 | ||||
| 	// if EOF, fh returns false
 | ||||
| 	while( fh ) { | ||||
| 		fh.getline(buf, bufsize); | ||||
| 
 | ||||
| 		charcount += strlen(buf)+1; // Newlines are not counted normally, but they are a character
 | ||||
| 		linecount++; | ||||
| 
 | ||||
| 		std::istringstream line(buf); | ||||
| 		while( line ) { | ||||
| 			line >> linebuf; | ||||
| 			wordcount++ ; | ||||
| 
 | ||||
| 			if ( line.eof() ) | ||||
| 			{ | ||||
| 				break ; | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 
 | ||||
| 	} | ||||
| 
 | ||||
| 	std::cout << " " << linecount-1 << " " << wordcount-1 << " " << charcount-1 << " " << argv[1] << std::endl; | ||||
| } | ||||
		Reference in a new issue
	
	 Eric Teunis de Boone
						Eric Teunis de Boone