Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. // ConsoleApplication5.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include<iostream>
  6. #include<fstream>
  7. #include<string>
  8. #include<cstdio>
  9.  
  10. #pragma warning(disable: 4996)
  11.  
  12. using namespace std;
  13. int number;
  14. //FILE* file;
  15. class LFile {
  16. private:
  17. string error;
  18. char* name;
  19. FILE* file1;
  20. public:
  21. LFile() {};
  22. LFile(char* name) {
  23. this->name = name;
  24. try {
  25. if (number < 5) {
  26. file1 = fopen(name, "a+");
  27. number++;
  28. cout << "Otwarto plik " << name << endl;
  29. }
  30. else {
  31. error = ("!!! Otwarto juz 5 plikow !!!");
  32. throw error;
  33. //fclose(file1);
  34. }
  35. }
  36. catch (string napis) {
  37. cout << napis << endl;
  38. }
  39. }
  40.  
  41. void readFile()
  42. {
  43. try {
  44. if (file1) {
  45. const int LICZBA_ELEMENTOW = 1000;
  46. char bufor[LICZBA_ELEMENTOW];
  47. size_t odczytanychBajtow = fread(bufor, sizeof(char), LICZBA_ELEMENTOW, file1);
  48. cout << "\nZawartosc pliku " << name << ":" << endl << endl;
  49. for (int i = 0; i < (int)odczytanychBajtow; i++)
  50. cout << bufor[i];
  51. cout << endl << endl;
  52. }
  53. else {
  54. error = ("!!! Plik nie zostal otwarty !!!");
  55. throw error;
  56. }
  57. }
  58. catch (string napis) {
  59. cout << napis << endl;
  60. }
  61. }
  62.  
  63. void writeFile()
  64. {
  65. try {
  66. if (!(file1)) {
  67. error = ("!!! Plik nie zostal otwarty !!!");
  68. throw error;
  69. }
  70. else {
  71. const int LICZBA_ELEMENTOW = 1000;
  72. char bufor[LICZBA_ELEMENTOW];
  73. cout << "Wpisz slowo, ktore ma sie znalezc w pliku: ";
  74. cin >> bufor;
  75. size_t zapisanychElementow = fwrite(bufor, sizeof(char), strlen(bufor), file1);
  76. cout << "Wyraz zapisano do pliku " << name << endl << endl;
  77. }
  78. }
  79. catch (string napis) {
  80. cout << napis << endl;
  81. }
  82. }
  83.  
  84. ~LFile(){
  85. if (file1) {
  86. fclose(file1);
  87. number--;
  88. }
  89. };
  90.  
  91. static int openFiles() {
  92. return number;
  93. }
  94. };
  95.  
  96. int main()
  97. {
  98. cout << "Liczba otwartych plikow: " << LFile().openFiles() << endl;
  99.  
  100. LFile plik1("pl1.txt"), plik2("pl2.txt"), plik3("pl3.txt"), plik4("pl4.txt"), plik5("pl5.txt"), plik6("pl6.txt");
  101.  
  102. // plik1.readFile();
  103. // plik1.writeFile();
  104.  
  105. // plik2.readFile();
  106. // plik2.writeFile();
  107.  
  108. // plik3.readFile();
  109. // plik3.writeFile();
  110.  
  111. // plik4.readFile();
  112. // plik4.writeFile();
  113.  
  114. // plik5.readFile();
  115. // plik5.writeFile();
  116.  
  117. // plik6.readFile();
  118. // plik6.writeFile();
  119.  
  120. plik1.readFile();
  121. plik3.writeFile();
  122.  
  123. /*plik1.~LFile();
  124. plik2.~LFile();
  125. plik3.~LFile();
  126. plik4.~LFile();
  127. plik5.~LFile();*/
  128.  
  129. cout << "Liczba otwartych plikow: " << LFile().openFiles() << endl;
  130. return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement