C++ - File management


SUBMITTED BY: Guest

DATE: Nov. 30, 2014, 4:16 p.m.

FORMAT: C++

SIZE: 4.0 kB

HITS: 5374

  1. //To be compiled using VS 2013
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <fstream>
  5. #include <windows.h>
  6. #include <string>
  7. using namespace std;
  8. void createFile(string path) {
  9. string fullpath = path;
  10. ofstream d(path);
  11. if (d.good() == true)
  12. {
  13. d.close();
  14. MessageBox(NULL, L"Action done successfully!", L"Done!", MB_OK | MB_ICONINFORMATION);
  15. }
  16. else MessageBox(NULL, L"Error while creating file!", NULL, MB_OK | MB_ICONSTOP);
  17. }
  18. void deleteFile(string path) {
  19. const char* fullpathc = path.c_str();
  20. if (remove(fullpathc) != 0)
  21. {
  22. MessageBox(NULL, L"Error while deleting file!", NULL, MB_OK | MB_ICONSTOP);
  23. }
  24. else
  25. {
  26. MessageBox(NULL, L"File deleted successfully!", L"Done!", MB_OK | MB_ICONINFORMATION);
  27. }
  28. }
  29. void writeToFile(string path, string toWrite, bool isNew)
  30. {
  31. const char* dane = toWrite.c_str();
  32. ofstream d;
  33. d.open(path, ofstream::out | ofstream::app);
  34. if (d.good() == true)
  35. {
  36. d.seekp(0, d.end);
  37. if (isNew == true)
  38. {
  39. d.write("\n", 1);
  40. }
  41. d.write(dane, toWrite.length());
  42. d.close();
  43. MessageBox(NULL, L"Action done successfully!", L"Done!", MB_OK | MB_ICONINFORMATION);
  44. }
  45. else MessageBox(NULL, L"Error while accessing file!", NULL, MB_OK | MB_ICONSTOP);
  46. }
  47. void printFile(string path)
  48. {
  49. ifstream d;
  50. string dane = "";
  51. d.open(path, ifstream::in);
  52. if (d.good() == true)
  53. {
  54. while (d.eof() == false) {
  55. getline(d, dane);
  56. cout << dane << endl;
  57. }
  58. d.close();
  59. _getch();
  60. }
  61. else MessageBox(NULL, L"Error while accessing file!", NULL, MB_OK | MB_ICONSTOP);
  62. }
  63. void renameFile(string pathsz, string newnames)
  64. {
  65. size_t found = pathsz.find_last_of("\\");
  66. const char* path = pathsz.c_str();
  67. newnames.insert(0, pathsz, 0, found + 1);
  68. newnames = newnames;
  69. const char* newname = newnames.c_str();
  70. int result = rename(path, newname);
  71. if (result == 0)
  72. {
  73. MessageBox(NULL, L"Action done successfully!", L"Done!", MB_OK | MB_ICONINFORMATION);
  74. }
  75. else MessageBox(NULL, L"Error while renaming file!", NULL, MB_OK | MB_ICONSTOP);
  76. }
  77. int main()
  78. {
  79. unsigned char wybor;
  80. string buf1;
  81. string buf3;
  82. int isNew;
  83. bool isNewB;
  84. do
  85. {
  86. system("cls");
  87. cin.clear();
  88. cin.sync();
  89. buf1 = "";
  90. buf3 = "";
  91. isNew = 0;
  92. isNewB = false;
  93. cout << "File managing. Press:\n1 - create a file\n2 - delete a file\n3 - write to an existent file\n4 - read from a file\n5 - rename a file\n0 - quit\nChoose an option: ";
  94. wybor = _getch();
  95. system("cls");
  96. switch (wybor)
  97. {
  98. case '1':
  99. {
  100. cout << "Give your precious file a path: ";
  101. getline(cin, buf1);
  102. createFile(buf1);
  103. }
  104. break;
  105. case '2':
  106. {
  107. cout << "Path of file to delete: ";
  108. getline(cin, buf1);
  109. deleteFile(buf1);
  110. }
  111. break;
  112. case '3':
  113. {
  114. cout << "Path of file to write to: ";
  115. getline(cin, buf1);
  116. cout << "Data to write to file: ";
  117. getline(cin, buf3);
  118. cout << "Do you wish to write the data in a new line?\n0 is no, 1 is yes: ";
  119. while (!(cin >> isNew)) {
  120. cout << "Incorrect data!";
  121. cin.clear();
  122. cin.sync();
  123. }
  124. if (isNew != 0)
  125. {
  126. isNewB = true;
  127. }
  128. writeToFile(buf1, buf3, isNewB);
  129. }
  130. break;
  131. case '4':
  132. {
  133. cout << "Path of file to read from: ";
  134. getline(cin, buf1);
  135. printFile(buf1);
  136. }
  137. break;
  138. case '5':
  139. {
  140. cout << "Path of file to rename: ";
  141. getline(cin, buf1);
  142. cout << "New name: ";
  143. getline(cin, buf3);
  144. renameFile(buf1, buf3);
  145. }
  146. break;
  147. }
  148. } while (wybor != '0');
  149. return 0;
  150. }

comments powered by Disqus