Website Summary Files Commits Branches @commit @diff @stats
path: root / week10 / Exercise03
diff options
context:
space:
mode:
Commit88f15e35713c9632216931d26443dc588238732f (patch)
Author
 Syndamia <kamen@syndamia.com>
2024-05-07 22:17:12 +0300
Committer
 Syndamia <kamen@syndamia.com>
2024-05-07 22:17:12 +0300
Files147ff8c72009afc58d3eabb1c026a33b798f20fe /week10/Exercise03
oop-2023-solutions-88f15e35713c9632216931d26443dc588238732f.tar
oop-2023-solutions-88f15e35713c9632216931d26443dc588238732f.tar.gz
oop-2023-solutions-88f15e35713c9632216931d26443dc588238732f.zip
Parentf4642325f58172e85b9e41e35f69e8bea46f78c6 (diff)
[w10] Added rough solutions to ex 1-10
1 files changed, 45 insertions, 0 deletions
-rw-r--r--week10/Exercise03/main.cpp45
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;
+ }
+}