lab 10: init

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2022-12-26 18:38:55 +05:30
parent 42902fa122
commit 9e7cffdc32
9 changed files with 298 additions and 2 deletions

4
.gitignore vendored
View File

@@ -4,4 +4,6 @@
_minted-file/ _minted-file/
.\# .\#
*html *html
lab*/*pdf lab*/*pdf
*.in
*.out

View File

@@ -44,3 +44,7 @@
* Lab 9 * Lab 9
#+INCLUDE: ./lab9/file.org #+INCLUDE: ./lab9/file.org
#+LATEX: \clearpage #+LATEX: \clearpage
* Lab 10
#+INCLUDE: ./lab10/file.org
#+LATEX: \clearpage

BIN
file.pdf

Binary file not shown.

26
lab10/28.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include <fstream>
#include <iostream>
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;
}

31
lab10/29.cpp Normal file
View File

@@ -0,0 +1,31 @@
#include <fstream>
#include <iostream>
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;
}

67
lab10/30.cpp Normal file
View File

@@ -0,0 +1,67 @@
#include <fstream>
#include <iostream>
#include <ostream>
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;
}

164
lab10/file.org Normal file
View File

@@ -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 <fstream>
#include <iostream>
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 <fstream>
#include <iostream>
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 <fstream>
#include <iostream>
#include <ostream>
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

View File

@@ -21,5 +21,6 @@ int main() {
file << line << endl; file << line << endl;
} }
file.close();
return 0; return 0;
} }

View File

@@ -81,7 +81,7 @@ Destructor called, nothing to do here.
#+LATEX: \clearpage #+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 #+ATTR_LATEX: :options frame=single,breaklines=true
#+begin_src cpp :tangle 27.cpp :wrap src text #+begin_src cpp :tangle 27.cpp :wrap src text
@@ -108,6 +108,7 @@ int main() {
file << line << endl; file << line << endl;
} }
file.close();
return 0; return 0;
} }
#+end_src #+end_src