aboutsummaryrefslogtreecommitdiff
path: root/week10/Exercise03/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'week10/Exercise03/main.cpp')
-rw-r--r--week10/Exercise03/main.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/week10/Exercise03/main.cpp b/week10/Exercise03/main.cpp
new file mode 100644
index 0000000..d394cbb
--- /dev/null
+++ b/week10/Exercise03/main.cpp
@@ -0,0 +1,45 @@
+#include <iostream>
+#include <fstream>
+
+void firstRow(const char* fileName) {
+ std::ifstream inFile(fileName);
+ if (!inFile.is_open()) {
+ throw "Couldn't open file!";
+ }
+
+ while (!inFile.eof() && inFile.peek() != '\n') {
+ inFile.get();
+ }
+ if (inFile.eof()) {
+ inFile.seekg(0, std::ios::end);
+ unsigned len = inFile.tellg();
+ inFile.close();
+ throw len;
+ }
+
+ inFile.seekg(0, std::ios::beg);
+ while (inFile.peek() != '\n') {
+ std::cout << inFile.get();
+ }
+
+ inFile.close();
+}
+
+int main() {
+ char buffer[1024];
+
+ while (true) {
+ try {
+ std::cin.getline(buffer, 1024);
+ firstRow(buffer);
+ }
+ catch (const char* couldntOpen) {
+ std::cout << couldntOpen;
+ }
+ catch (unsigned fileSize) {
+ std::cout << "In " << fileSize << " characters, there is now newline!" << std::endl;
+ continue;
+ }
+ break;
+ }
+}