File Handling
#include <iostream>
#include <fstream>
using namespace std;
class File
{
string ItemName;
int code;
float price;
public:
File(); // Constructer
~File(){}; // Destructer
void writeData(int);
void readData();
};
File::File()
{
ItemName = "";
code = 0;
price = 0.0;
}
// Function to Handle Wrong Input
template <typename T>
T checkInput(T Input, string content)
{
while (!cin.good())
{
cout << "\nWrong Input, Please Enter It Correctly!!\n";
cin.clear();
cin.ignore(100, '\n');
cout << content;
cin >> Input;
}
while (Input < 0)
{
cout << "\nThis Input Cannot Be Negative!!\n";
cin.clear();
cin.ignore(100, '\n');
cout << content;
cin >> Input;
}
return Input;
}
void File ::writeData(int choice)
{
char userInput;
cout << "\n***** Product Details *****\n";
string content1("\nEnter The Code : ");
string content2("\nEnter The Price : ");
ofstream outFile;
if (choice == 1)
outFile.open("File.txt", ios::out);
else
outFile.open("File.txt", ios::app);
while (true)
{
if (outFile.is_open())
{
cout << "\nEnter The Name of Product : ";
cin.ignore(100, '\n');
getline(cin, ItemName);
cout << content1;
cin >> code;
code = checkInput(code, content1);
cout << content2;
cin >> price;
price = checkInput(price, content2);
outFile << "\nName : " << ItemName << endl;
outFile << "Code : " << code << endl;
outFile << "Price : " << price << "/-" << endl;
}
else
cout << "\nThere is Error While Opening The File\n";
cout << "\nPress 'c' To Continue Writing in The File Else Press 'e'\n";
cin >> userInput;
label1:
switch (userInput)
{
case 'c':
case 'C':
break;
case 'e':
case 'E':
return;
default:
cout << "\nPlease Enter Correct Input!!\n";
goto label1;
break;
}
}
outFile.close();
}
bool inline isEmpty(ifstream &eFile)
{
return eFile.peek() == ifstream::traits_type::eof();
}
void File::readData()
{
string content;
ifstream inFile;
inFile.open("File.txt", ios::in);
if (isEmpty(inFile))
{
cout << "\nThe File is Empty\n";
}
else if (inFile.is_open())
{
while (!inFile.eof())
{
getline(inFile, content);
cout << endl
<< content;
}
}
else
{
cout << "\nThere is Error in Opening The File\n";
}
inFile.close();
}
int main()
{
File *file = new (nothrow) File(); // Implicitely nothrow will convert the pointer into false if the memory is not allocated
if (!file)
throw bad_alloc();
int userInput;
string content("\n1. Press 1 To Write File In The Output Mode\n2. Press 2 To Write File In The Append Mode\n3. Press 3 To Read File\n4. Press 4 To Exit\n>>>> ");
while (true)
{
cout << content;
cin >> userInput;
userInput = checkInput(userInput, content);
switch (userInput)
{
case 1:
file->writeData(1);
cout << "\nContent Successfully Added in The File\n";
break;
case 2:
file->writeData(2);
cout << "\nContent Successfully Added in The File\n";
break;
case 3:
file->readData();
break;
case 4:
exit(0);
break;
default:
cout << "\nPlease Enter Correct Input\n";
break;
}
}
return 0;
}
// Output
// 1. Press 1 To Write File In The Output Mode
// 2. Press 2 To Write File In The Append Mode
// 3. Press 3 To Read File
// 4. Press 4 To Exit
// >>>> 3
// The File is Empty
// 1. Press 1 To Write File In The Output Mode
// 2. Press 2 To Write File In The Append Mode
// 3. Press 3 To Read File
// 4. Press 4 To Exit
// >>>> 1
// ***** Product Details *****
// Enter The Name of Product : Pen
// Enter The Code : 1
// Enter The Price : 5
// Press 'c' To Continue Writing in The File Else Press 'e'
// c
// Enter The Name of Product : Pencil
// Enter The Code : 2
// Enter The Price : 6
// Press 'c' To Continue Writing in The File Else Press 'e'
// e
// Content Successfully Added in The File
// 1. Press 1 To Write File In The Output Mode
// 2. Press 2 To Write File In The Append Mode
// 3. Press 3 To Read File
// 4. Press 4 To Exit
// >>>> 3
// Name : Pen
// Code : 1
// Price : 5/-
// Name : Pencil
// Code : 2
// Price : 6/-
// 1. Press 1 To Write File In The Output Mode
// 2. Press 2 To Write File In The Append Mode
// 3. Press 3 To Read File
// 4. Press 4 To Exit
// >>>> 2
// ***** Product Details *****
// Enter The Name of Product : Scale
// Enter The Code : 3
// Enter The Price : 10
// Press 'c' To Continue Writing in The File Else Press 'e'
// e
// Content Successfully Added in The File
// 1. Press 1 To Write File In The Output Mode
// 2. Press 2 To Write File In The Append Mode
// 3. Press 3 To Read File
// 4. Press 4 To Exit
// >>>> 3
// Name : Pen
// Code : 1
// Price : 5/-
// Name : Pencil
// Code : 2
// Price : 6/-
// Name : Scale
// Code : 3
// Price : 10/-
// 1. Press 1 To Write File In The Output Mode
// 2. Press 2 To Write File In The Append Mode
// 3. Press 3 To Read File
// 4. Press 4 To Exit
// >>>> 4
Comments
Post a Comment