From a54f03b178f4017c254096d654b0de0d5b48a0c3 Mon Sep 17 00:00:00 2001 From: Eric Teunis de Boone Date: Tue, 17 Dec 2019 18:05:41 +0100 Subject: [PATCH] Ex5: Input Streams --- ex5.1/readfromstdin.cpp | 43 ++++++++++++++++++++++++++++++++++ ex5.2/main.cpp | 35 +++++++++++++++++++++++++++ ex5.3/main.cpp | 52 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 ex5.1/readfromstdin.cpp create mode 100644 ex5.2/main.cpp create mode 100644 ex5.3/main.cpp diff --git a/ex5.1/readfromstdin.cpp b/ex5.1/readfromstdin.cpp new file mode 100644 index 0000000..cfd1404 --- /dev/null +++ b/ex5.1/readfromstdin.cpp @@ -0,0 +1,43 @@ +#include +#include + +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; +} diff --git a/ex5.2/main.cpp b/ex5.2/main.cpp new file mode 100644 index 0000000..8aca7bd --- /dev/null +++ b/ex5.2/main.cpp @@ -0,0 +1,35 @@ +#include +#include + + +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; + } + } + + +} diff --git a/ex5.3/main.cpp b/ex5.3/main.cpp new file mode 100644 index 0000000..1d8ea50 --- /dev/null +++ b/ex5.3/main.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include + +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; +}