Don't like ads? PRO users don't see any ads ;-)
Guest

jejerome

By: a guest on Apr 18th, 2012  |  syntax: C++  |  size: 2.58 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # include <iostream>
  2. # include <fstream>// file stream module
  3. #include <string>
  4. /*
  5.  
  6. needed text files
  7. myfilename.txt // book list
  8. */
  9. using namespace std;
  10. const int maxpend =2;
  11. const int size = 50;
  12. char quest;
  13. int cnt=0;
  14. int tail=0;
  15. string search0;
  16. char borrowerid[size];
  17. int bookidr[ size ];
  18. char Title[ size ];
  19. int bookd;
  20.  string titlex[size];
  21. void menu() // menu options
  22. {  
  23.      system("cls"); // clear screen function
  24.    
  25.      cout << "               FileStream and FileWrite Demonstration           "<<endl<<endl;
  26.      cout<<"                 total Lines In text  "<<tail;
  27.      cout << "               Main Menu :            "<<endl<<endl;
  28.      cout << "               Enter Choice"<<endl<<endl;
  29.      cout << "               1-Read Module"<<endl;
  30.      cout << "               2-Write Module"<<endl;
  31.     // cout << "               3-Modify Text File"<<endl;
  32.      cout << "               7-Exit"<<endl;
  33. }
  34.  
  35. void txtopenbook()
  36. {
  37.       cnt=0; // bug fix reset fileread module index counter // credits goes to jeJerome
  38. ofstream outfile;
  39. outfile.open("myfilename.txt",ios::app);
  40. outfile.close();
  41. ifstream infile;
  42. infile.open("myfilename.txt");
  43. while ( infile ) // run till end of the text file
  44. {
  45.  
  46.    if (infile == false)
  47.    cout<<"Could not open the input file"<<endl;
  48.    // read another record
  49.    infile.getline( Title, size ); cnt++;
  50. // char to string
  51.    titlex[cnt]=Title;
  52.    tail = cnt-1; // records teh last element
  53.  
  54. }
  55.  
  56.  
  57. }
  58. void filewrite()
  59. {
  60. ofstream outfile;
  61. outfile.open("myfilename.txt",ios::app);
  62.  for(int ctr1 = 1; ctr1<cnt; ctr1++)
  63. {          cout<<"Now Writing Line "<<ctr1<<" :  "<<titlex[ctr1]<<endl;
  64.            outfile<<titlex[ctr1]<<endl;
  65.            cout<<endl;
  66.       }
  67. }
  68.  
  69. void arraymanip()
  70. {
  71.  
  72. cout<<" pls enter Line Number "<<endl;
  73. int input0;
  74. cin>>input0;
  75. if(input0>tail) {cout<<"invalid line number"<<endl; return;  }
  76. cout<<" pls enter string value"<<endl;
  77. string input1;
  78. cin>>input1;
  79.  
  80. titlex[input0] = input1;
  81. cout<<"done"<<endl;
  82. filewrite();
  83. }
  84.  
  85.  
  86. void viewbookdb() // Print Converted Arrays
  87. {
  88.  for(int ctr1 = 1; ctr1<cnt; ctr1++)
  89. {          cout<<"Now Printing Line "<<ctr1<<" :  "<<titlex[ctr1]<<endl;
  90.      //       cout<<"Now Printing Line "<<ctr1<<" :  "<<titlex[ctr1]<<endl;
  91.            cout<<endl;
  92.       }
  93.    system ("pause");
  94. }
  95.  
  96.  
  97. int main()
  98. {
  99.  // open text file
  100. for(;;)
  101. {
  102.       // reset fileread Module
  103.        txtopenbook();
  104.        menu() ; // menu function
  105.  
  106. cin>>quest;
  107. if(quest=='1'){ viewbookdb();}
  108. //if(quest=='2'){ filewrite();}
  109. if(quest=='2'){arraymanip();}
  110. if(quest=='7'){ exit(0);}
  111.        }
  112. }