diff --git a/.gitignore b/.gitignore index 9550cb5..e3a05d0 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,5 @@ relative/path/to/dir/anotherfile # Ignore vscode files */.vscode/ + +*.exe diff --git a/FileHandling/README.md b/FileHandling/README.md index e69de29..73c94aa 100644 --- a/FileHandling/README.md +++ b/FileHandling/README.md @@ -0,0 +1,7 @@ + +- [x] [q1](q1.cpp): WAP to open a file using constructor. +- [x] [q2](q2.cpp): WAP to open a file using member function. +- [x] [q3](q3.cpp): WAP to user r/w file using fstream. +- [x] [q4](q4.cpp): WAP using fin and fout. +- [x] [q5](q5.cpp): WAP to show use of manipulators. +- [x] [q6](q6.cpp): WAP to store book database in a file. Design a book class with members ISBN number, name of author, title, price. The Program should able to store book object to a file, find a book in the database and display according to ISBN number. Also display all records. \ No newline at end of file diff --git a/FileHandling/q1.cpp b/FileHandling/q1.cpp new file mode 100644 index 0000000..286d716 --- /dev/null +++ b/FileHandling/q1.cpp @@ -0,0 +1,43 @@ +// WAP to open a file using constructor. + +#include +#include + +class FileOpener { +private: + std::ifstream file; // Input file stream + +public: + // Constructor that takes a filename as an argument and opens the file + FileOpener(const std::string& filename) { + file.open(filename, std::ios::in); // Open the file in read mode + + if (!file) { + std::cerr << "Error opening file: " << filename << std::endl; + exit(1); // Exit the program if file opening fails + } else { + std::cout << "File " << filename << " opened successfully!" << std::endl; + } + } + + // Destructor to close the file + ~FileOpener() { + if (file.is_open()) { + file.close(); + std::cout << "File closed successfully!" << std::endl; + } + } +}; + +int main() { + std::string filename; + + std::cout << "Enter the filename to open: "; + std::cin >> filename; + + FileOpener fileOpener(filename); // This will open the file using the constructor + + // Do any operations with the file here... + + return 0; // File will be closed automatically when the object goes out of scope +} diff --git a/FileHandling/q2.cpp b/FileHandling/q2.cpp new file mode 100644 index 0000000..524944f --- /dev/null +++ b/FileHandling/q2.cpp @@ -0,0 +1,43 @@ +// WAP to open a file using member function. + +#include +#include + +class FileHandler { +private: + std::ifstream file; // Input file stream + +public: + // Member function to open a file + void openFile(const std::string& filename) { + file.open(filename, std::ios::in); // Open the file in read mode + + if (!file) { + std::cerr << "Error opening file: " << filename << std::endl; + } else { + std::cout << "File " << filename << " opened successfully!" << std::endl; + } + } + + // Destructor to close the file + ~FileHandler() { + if (file.is_open()) { + file.close(); + std::cout << "File closed successfully!" << std::endl; + } + } +}; + +int main() { + std::string filename; + + std::cout << "Enter the filename to open: "; + std::cin >> filename; + + FileHandler fileHandler; + fileHandler.openFile(filename); // This will open the file using the member function + + // Do any operations with the file here... + + return 0; // File will be closed automatically when the object goes out of scope +} diff --git a/FileHandling/q3.cpp b/FileHandling/q3.cpp new file mode 100644 index 0000000..741f5b1 --- /dev/null +++ b/FileHandling/q3.cpp @@ -0,0 +1,63 @@ +// WAP to user r/w file using fstream. + +#include +#include +#include + +class FileHandler { +private: + std::fstream file; // File stream for both reading and writing + +public: + // Member function to open a file in read/write mode + void openFile(const std::string& filename) { + file.open(filename, std::ios::in | std::ios::out | std::ios::app); // Open file in read/write mode, and append mode + + if (!file) { + std::cerr << "Error opening file: " << filename << std::endl; + exit(1); + } + } + + // Member function to write data to the file + void writeToFile(const std::string& data) { + file << data << std::endl; + } + + // Member function to read data from the file + void readFromFile() { + std::string line; + while (getline(file, line)) { + std::cout << line << std::endl; + } + } + + // Member function to reset the file pointer to the beginning + void resetFilePointer() { + file.seekg(0, std::ios::beg); + } + + // Destructor to close the file + ~FileHandler() { + if (file.is_open()) { + file.close(); + } + } +}; + +int main() { + FileHandler fileHandler; + fileHandler.openFile("sample.txt"); + + // Writing to the file + fileHandler.writeToFile("Hello, World!"); + + // Reset file pointer to the beginning for reading + fileHandler.resetFilePointer(); + + // Reading from the file + std::cout << "Contents of the file:" << std::endl; + fileHandler.readFromFile(); + + return 0; +} diff --git a/FileHandling/q4.cpp b/FileHandling/q4.cpp new file mode 100644 index 0000000..495d119 --- /dev/null +++ b/FileHandling/q4.cpp @@ -0,0 +1,52 @@ +// WAP using fin and fout. + +#include +#include +#include + +class FileHandler { +public: + // Member function to write data to the file using ofstream (fout) + void writeToFile(const std::string& filename, const std::string& data) { + std::ofstream fout(filename, std::ios::app); // Open file in append mode + + if (!fout) { + std::cerr << "Error opening file for writing: " << filename << std::endl; + exit(1); + } + + fout << data << std::endl; + fout.close(); + } + + // Member function to read data from the file using ifstream (fin) + void readFromFile(const std::string& filename) { + std::ifstream fin(filename); // Open file in read mode + + if (!fin) { + std::cerr << "Error opening file for reading: " << filename << std::endl; + exit(1); + } + + std::string line; + while (getline(fin, line)) { + std::cout << line << std::endl; + } + + fin.close(); + } +}; + +int main() { + FileHandler fileHandler; + std::string filename = "sample.txt"; + + // Writing to the file using fout + fileHandler.writeToFile(filename, "Hello, World!"); + + // Reading from the file using fin + std::cout << "Contents of the file:" << std::endl; + fileHandler.readFromFile(filename); + + return 0; +} diff --git a/FileHandling/q5.cpp b/FileHandling/q5.cpp new file mode 100644 index 0000000..b7c5a16 --- /dev/null +++ b/FileHandling/q5.cpp @@ -0,0 +1,29 @@ +// WAP to show use of manipulators. + +#include +#include // Required for manipulators like setw, setprecision, etc. + +int main() { + int number = 12345; + double pi = 3.141592653589793; + + std::cout << "Without setw:" << std::endl; + std::cout << number << std::endl; + std::cout << pi << std::endl; + + std::cout << "\nWith setw:" << std::endl; + std::cout << std::setw(10) << number << std::endl; // Right aligns the number with a width of 10 + std::cout << std::setw(10) << pi << std::endl; // Right aligns the value of pi with a width of 10 + + std::cout << "\nWith setprecision:" << std::endl; + std::cout << std::setprecision(5) << pi << std::endl; // Displays pi with a total of 5 digits + std::cout << std::setprecision(9) << pi << std::endl; // Displays pi with a total of 9 digits + + std::cout << "\nWith fixed and setprecision:" << std::endl; + std::cout << std::fixed << std::setprecision(2) << pi << std::endl; // Displays pi with exactly 2 digits after the decimal point + + std::cout << "\nWith showpos:" << std::endl; + std::cout << std::showpos << number << std::endl; // Displays the positive sign before positive numbers + + return 0; +} diff --git a/FileHandling/q6.cpp b/FileHandling/q6.cpp new file mode 100644 index 0000000..3ebb77f --- /dev/null +++ b/FileHandling/q6.cpp @@ -0,0 +1,173 @@ +// WAP to store book database in a file. Design a book class with members ISBN number, name of author, title, price. The Program should able to store book object to a file, find a book in the database and display according to ISBN number. Also display all records. +// getInput() +// display() +// add_record +// show_record +// search_record_by_isbn_number & modify also. + +#include +#include +#include +#include + +class Book { + std::string isbn; + std::string author; + std::string title; + double price; + +public: + // Constructors + Book() {} + Book(const std::string &isbn, const std::string &author, const std::string &title, double price) + : isbn(isbn), author(author), title(title), price(price) {} + + // Member functions + void getInput() { + std::cout << "Enter ISBN: "; + std::cin >> isbn; + std::cin.ignore(); // Clear the newline character from the buffer + std::cout << "Enter Author: "; + std::getline(std::cin, author); + std::cout << "Enter Title: "; + std::getline(std::cin, title); + std::cout << "Enter Price: "; + std::cin >> price; + } + + void display() const { + std::cout << "ISBN: " << isbn << ", Author: " << author << ", Title: " << title << ", Price: $" << price << std::endl; + } + + std::string getISBN() const { return isbn; } + + // Overloading extraction and insertion operators for file operations + friend std::ofstream &operator<<(std::ofstream &ofs, const Book &book); + friend std::ifstream &operator>>(std::ifstream &ifs, Book &book); +}; + +std::ofstream &operator<<(std::ofstream &ofs, const Book &book) { + ofs << book.isbn << "\n" << book.author << "\n" << book.title << "\n" << book.price << "\n"; + return ofs; +} + +std::ifstream &operator>>(std::ifstream &ifs, Book &book) { + getline(ifs, book.isbn); + getline(ifs, book.author); + getline(ifs, book.title); + ifs >> book.price; + ifs.ignore(); // Clear the newline character from the buffer + return ifs; +} + +class BookDatabase { + std::string filename; + +public: + BookDatabase(const std::string &filename) : filename(filename) {} + + void add_record() { + Book book; + book.getInput(); + + std::ofstream ofs(filename, std::ios::app); // Append mode + ofs << book; + ofs.close(); + } + + void show_records() { + std::ifstream ifs(filename); + Book book; + while (ifs >> book) { + book.display(); + } + ifs.close(); + } + + void search_record_by_isbn_number() { + std::string search_isbn; + std::cout << "Enter ISBN to search: "; + std::cin >> search_isbn; + std::cin.ignore(); // Clear the newline character from the buffer + + std::ifstream ifs(filename); + Book book; + bool found = false; + while (ifs >> book) { + if (book.getISBN() == search_isbn) { + std::cout << "Book found:\n"; + book.display(); + found = true; + break; + } + } + ifs.close(); + + if (!found) { + std::cout << "Book with ISBN " << search_isbn << " not found.\n"; + } + } + + void modify_record() { + std::vector books; + std::string search_isbn; + std::cout << "Enter ISBN to modify: "; + std::cin >> search_isbn; + std::cin.ignore(); // Clear the newline character from the buffer + + bool found = false; + std::ifstream ifs(filename); + Book book; + while (ifs >> book) { + if (book.getISBN() == search_isbn) { + std::cout << "Book found:\n"; + book.display(); + std::cout << "\nEnter new details for the book:\n"; + book.getInput(); + found = true; + } + books.push_back(book); + } + ifs.close(); + + std::ofstream ofs(filename); // Overwrite mode + for (const auto &b : books) { + ofs << b; + } + ofs.close(); + + if (!found) { + std::cout << "Book with ISBN " << search_isbn << " not found.\n"; + } else { + std::cout << "Book details updated successfully.\n"; + } + } +}; + +int main() { + BookDatabase db("books.txt"); + + int choice; + do { + std::cout << "\nMenu:\n"; + std::cout << "1. Add Record\n"; + std::cout << "2. Show Records\n"; + std::cout << "3. Search Record by ISBN\n"; + std::cout << "4. Modify Record\n"; + std::cout << "5. Exit\n"; + std::cout << "Enter choice: "; + std::cin >> choice; + std::cin.ignore(); // Clear the newline character from the buffer + + switch (choice) { + case 1: db.add_record(); break; + case 2: db.show_records(); break; + case 3: db.search_record_by_isbn_number(); break; + case 4: db.modify_record(); break; + case 5: std::cout << "Goodbye!\n"; break; + default: std::cout << "Invalid choice!\n"; + } + } while (choice != 5); + + return 0; +}