# include <iostream>
# include <fstream>// file stream module
#include <string>
/*
needed text files
myfilename.txt // book list
*/
using namespace std;
const int maxpend =2;
const int size = 50;
char quest;
int cnt=0;
int tail=0;
string search0;
char borrowerid[size];
int bookidr[ size ];
char Title[ size ];
int bookd;
string titlex[size];
void menu() // menu options
{
system("cls"); // clear screen function
cout << " FileStream and FileWrite Demonstration "<<endl<<endl;
cout<<" total Lines In text "<<tail;
cout << " Main Menu : "<<endl<<endl;
cout << " Enter Choice"<<endl<<endl;
cout << " 1-Read Module"<<endl;
cout << " 2-Write Module"<<endl;
// cout << " 3-Modify Text File"<<endl;
cout << " 7-Exit"<<endl;
}
void txtopenbook()
{
cnt=0; // bug fix reset fileread module index counter // credits goes to jeJerome
ofstream outfile;
outfile.open("myfilename.txt",ios::app);
outfile.close();
ifstream infile;
infile.open("myfilename.txt");
while ( infile ) // run till end of the text file
{
if (infile == false)
cout<<"Could not open the input file"<<endl;
// read another record
infile.getline( Title, size ); cnt++;
// char to string
titlex[cnt]=Title;
tail = cnt-1; // records teh last element
}
}
void filewrite()
{
ofstream outfile;
outfile.open("myfilename.txt",ios::app);
for(int ctr1 = 1; ctr1<cnt; ctr1++)
{ cout<<"Now Writing Line "<<ctr1<<" : "<<titlex[ctr1]<<endl;
outfile<<titlex[ctr1]<<endl;
cout<<endl;
}
}
void arraymanip()
{
cout<<" pls enter Line Number "<<endl;
int input0;
cin>>input0;
if(input0>tail) {cout<<"invalid line number"<<endl; return; }
cout<<" pls enter string value"<<endl;
string input1;
cin>>input1;
titlex[input0] = input1;
cout<<"done"<<endl;
filewrite();
}
void viewbookdb() // Print Converted Arrays
{
for(int ctr1 = 1; ctr1<cnt; ctr1++)
{ cout<<"Now Printing Line "<<ctr1<<" : "<<titlex[ctr1]<<endl;
// cout<<"Now Printing Line "<<ctr1<<" : "<<titlex[ctr1]<<endl;
cout<<endl;
}
system ("pause");
}
int main()
{
// open text file
for(;;)
{
// reset fileread Module
txtopenbook();
menu() ; // menu function
cin>>quest;
if(quest=='1'){ viewbookdb();}
//if(quest=='2'){ filewrite();}
if(quest=='2'){arraymanip();}
if(quest=='7'){ exit(0);}
}
}