Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. `#include <iostream>`
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7. void show() // this function displays the file content
  8. {
  9.  
  10. string file_nm, STRING;
  11. int i =0;
  12. cout<<"Please enter a file name: ";
  13. cin>>file_nm;
  14.  
  15. ifstream infile;
  16. if (infile.good())
  17. {
  18. infile.open ("file_nm");
  19. while(!infile.eof()) // To get you all the lines.
  20. {
  21. getline(infile,STRING); // Saves the line in STRING.
  22. cout<<STRING; // Prints our STRING.
  23. }
  24. infile.close();
  25. }
  26. else
  27. {
  28. cout<< "Error opening the file: ";
  29. }
  30.  
  31.  
  32. i++;
  33. if (i%20==0)
  34. {
  35. system("pause");
  36. }
  37.  
  38. }
  39.  
  40.  
  41. void add() // Appends to the end of the file
  42. {
  43. string txt, file_nm;
  44. cout<<"Enter a file name: ";
  45. cin>> file_nm;
  46. cout<<"Please enter a name to add to the file: ";
  47. cin>> txt;
  48. std::ofstream outfile;
  49. outfile.open(file_nm.c_str());
  50. outfile << file_nm;
  51. {
  52. outfile.close();
  53. }
  54. system("Pause");
  55. }
  56. int main(int argc, char *argv[])
  57. {
  58. //show();
  59.  
  60. int cont = 1, choose;
  61.  
  62. while(cont==1)
  63.  
  64. {
  65.  
  66. cout << "Main menun";
  67. cout <<"1) Show a listn";
  68. cout << "2) Add a namen";
  69. cout << "3) Exitn";
  70. cout<<" Please enter which option you want: "<<endl;
  71. cin >> choose;
  72. switch (choose)
  73. {
  74. case 1:
  75. show();
  76. break;
  77. case 2:
  78. add();
  79. break;
  80. case 3:
  81. cont=2;
  82. break;
  83. default:
  84. cout<<"BAD INPUT"<<endl;
  85. break;
  86. }
  87.  
  88.  
  89.  
  90. }
  91. return EXIT_SUCCESS;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement