diff --git a/.gitignore b/.gitignore index d9541d6..78f61d4 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ _minted-file/ .\# *html -lab*/*pdf \ No newline at end of file +lab*/*pdf +*.in +*.out \ No newline at end of file diff --git a/file.org b/file.org index 3594970..7359163 100644 --- a/file.org +++ b/file.org @@ -44,3 +44,7 @@ * Lab 9 #+INCLUDE: ./lab9/file.org #+LATEX: \clearpage + +* Lab 10 +#+INCLUDE: ./lab10/file.org +#+LATEX: \clearpage diff --git a/file.pdf b/file.pdf index b1f7a7b..d350598 100644 Binary files a/file.pdf and b/file.pdf differ diff --git a/lab10/28.cpp b/lab10/28.cpp new file mode 100644 index 0000000..946e948 --- /dev/null +++ b/lab10/28.cpp @@ -0,0 +1,26 @@ +#include +#include + +using namespace std; +const string FILE_PATH = "./28.in"; + +int main() { + string line; + + fstream file; + file.open(FILE_PATH, ios_base::in); + + if (!file.is_open()) + exit(1); + + while (getline(file, line)) { + cout << line; + if (file.peek() != EOF) { + cout << endl; + } + } + + file.close(); + + return 0; +} diff --git a/lab10/29.cpp b/lab10/29.cpp new file mode 100644 index 0000000..0456b97 --- /dev/null +++ b/lab10/29.cpp @@ -0,0 +1,31 @@ +#include +#include + +using namespace std; +const string SRC_FILE_PATH = "./29.in"; +const string DST_FILE_PATH = "./29.out"; + +int main() { + string line; + + fstream src, dst; + src.open(SRC_FILE_PATH, ios_base::in); + dst.open(DST_FILE_PATH, ios_base::out); + dst.close(); + dst.open(DST_FILE_PATH, ios_base::app); + + if (!src.is_open()) + exit(1); + + while (getline(src, line)) { + dst << line; + if (src.peek() != EOF) { + dst << endl; + } + } + + src.close(); + dst.close(); + + return 0; +} diff --git a/lab10/30.cpp b/lab10/30.cpp new file mode 100644 index 0000000..7065136 --- /dev/null +++ b/lab10/30.cpp @@ -0,0 +1,67 @@ +#include +#include +#include + +using namespace std; +const string FILE_PATH = "./30.out"; + +class StudentInfo { + char name[30]; + uint age; + char sex; + uint height; +public: + void read(); + void write(); +}; + +void StudentInfo::read() { + cout << "Name: "; + cin >> name; + cout << "Age: "; + cin >> age; + cout << "Sex (m/f): "; + cin >> sex; + cout << "Height: "; + cin >> height; + cout << "[LOG] Data read\n"; +} + +void StudentInfo::write() { + ofstream file(FILE_PATH); + file.close(); + file.open(FILE_PATH, ios_base::app); + + file << "Name: " << name << endl + << "Age: " << age << endl + << "Sex: " << (sex == 'm' ? "Male" : "Female") << endl + << "Height: " << height << "cm"; + + cout << "[LOG] Data written\n"; + + file.close(); +} + +int main() { + StudentInfo foo; + string line; + + foo.read(); + foo.write(); + + ifstream file(FILE_PATH); + + if (!file.is_open()) + exit(1); + + cout << "File contents: \n"; + + while (getline(file, line)) { + cout << line; + if (file.peek() != EOF) { + cout << endl; + } + } + + return 0; +} diff --git a/lab10/file.org b/lab10/file.org new file mode 100644 index 0000000..f459735 --- /dev/null +++ b/lab10/file.org @@ -0,0 +1,164 @@ +* Write a program to read a text file and display its contents on the screen. + +#+ATTR_LATEX: :options frame=single,breaklines=true +#+begin_src cpp :tangle 28.cpp :wrap src text +#include +#include + +using namespace std; +const string FILE_PATH = "./28.in"; + +int main() { + string line; + + fstream file; + file.open(FILE_PATH, ios_base::in); + + if (!file.is_open()) + exit(1); + + while (getline(file, line)) { + cout << line; + if (file.peek() != EOF) { + cout << endl; + } + } + + file.close(); + + return 0; +} +#+end_src + +#+begin_src text +Asperger syndrome (AS), also known as Asperger's, is a former neurodevelopmental disorder. +It is characterized by significant difficulties in social interaction and nonverbal communication. +#+end_src + +#+LATEX: \clearpage + +* Write a program to copy the contents of a file into another. + +#+ATTR_LATEX: :options frame=single,breaklines=true +#+begin_src cpp :tangle 29.cpp :wrap src text +#include +#include + +using namespace std; +const string SRC_FILE_PATH = "./29.in"; +const string DST_FILE_PATH = "./29.out"; + +int main() { + string line; + + fstream src, dst; + src.open(SRC_FILE_PATH, ios_base::in); + dst.open(DST_FILE_PATH, ios_base::out); + dst.close(); + dst.open(DST_FILE_PATH, ios_base::app); + + if (!src.is_open()) + exit(1); + + while (getline(src, line)) { + dst << line; + if (src.peek() != EOF) { + dst << endl; + } + } + + src.close(); + dst.close(); + + return 0; +} +#+end_src + +#+LATEX: \clearpage + +* Write a program to read the class object of student_info such as name , age ,sex ,height and weight from the keyboard and to store them on a specified file using read() and write() functions. Again the same file is opened for reading and displaying the contents of the file on the screen. + +#+ATTR_LATEX: :options frame=single,breaklines=true +#+begin_src cpp :tangle 30.cpp :wrap src text +#include +#include +#include + +using namespace std; +const string FILE_PATH = "./30.out"; + +class StudentInfo { + char name[30]; + uint age; + char sex; + uint height; +public: + void read(); + void write(); +}; + +void StudentInfo::read() { + cout << "Name: "; + cin >> name; + cout << "Age: "; + cin >> age; + cout << "Sex (m/f): "; + cin >> sex; + cout << "Height: "; + cin >> height; + cout << "[LOG] Data read\n"; +} + +void StudentInfo::write() { + ofstream file(FILE_PATH); + file.close(); + file.open(FILE_PATH, ios_base::app); + + file << "Name: " << name << endl + << "Age: " << age << endl + << "Sex: " << (sex == 'm' ? "Male" : "Female") << endl + << "Height: " << height << "cm"; + + cout << "[LOG] Data written\n"; + + file.close(); +} + +int main() { + StudentInfo foo; + string line; + + foo.read(); + foo.write(); + + ifstream file(FILE_PATH); + + if (!file.is_open()) + exit(1); + + cout << "File contents: \n"; + + while (getline(file, line)) { + cout << line; + if (file.peek() != EOF) { + cout << endl; + } + } + + return 0; +} +#+end_src + +#+begin_src text +Name: Hello +Age: 19 +Sex (m/f): m +Height: 180 +[LOG] Data read +[LOG] Data written +File contents: +Name: Hello +Age: 19 +Sex: Male +Height: 180cm +#+end_src diff --git a/lab9/27.cpp b/lab9/27.cpp index 794d345..896e936 100644 --- a/lab9/27.cpp +++ b/lab9/27.cpp @@ -21,5 +21,6 @@ int main() { file << line << endl; } + file.close(); return 0; } diff --git a/lab9/file.org b/lab9/file.org index 7d8bb4b..c5b5d84 100644 --- a/lab9/file.org +++ b/lab9/file.org @@ -81,7 +81,7 @@ Destructor called, nothing to do here. #+LATEX: \clearpage -* Write a program to illustrate how template functions can be overloaded. +* Write a program to read a set of lines from the keyboard and to store it on a specified file. #+ATTR_LATEX: :options frame=single,breaklines=true #+begin_src cpp :tangle 27.cpp :wrap src text @@ -108,6 +108,7 @@ int main() { file << line << endl; } + file.close(); return 0; } #+end_src