-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from nawarajshah/file-handling
File handling
- Loading branch information
Showing
8 changed files
with
412 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,5 @@ relative/path/to/dir/anotherfile | |
|
||
# Ignore vscode files | ||
*/.vscode/ | ||
|
||
*.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// WAP to open a file using constructor. | ||
|
||
#include <iostream> | ||
#include <fstream> | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// WAP to open a file using member function. | ||
|
||
#include <iostream> | ||
#include <fstream> | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// WAP to user r/w file using fstream. | ||
|
||
#include <iostream> | ||
#include <fstream> | ||
#include <string> | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// WAP using fin and fout. | ||
|
||
#include <iostream> | ||
#include <fstream> | ||
#include <string> | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// WAP to show use of manipulators. | ||
|
||
#include <iostream> | ||
#include <iomanip> // 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; | ||
} |
Oops, something went wrong.