Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication5.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include<iostream>
- #include<fstream>
- #include<string>
- #include<cstdio>
- #pragma warning(disable: 4996)
- using namespace std;
- int number;
- //FILE* file;
- class LFile {
- private:
- string error;
- char* name;
- FILE* file1;
- public:
- LFile() {};
- LFile(char* name) {
- this->name = name;
- try {
- if (number < 5) {
- file1 = fopen(name, "a+");
- number++;
- cout << "Otwarto plik " << name << endl;
- }
- else {
- error = ("!!! Otwarto juz 5 plikow !!!");
- throw error;
- //fclose(file1);
- }
- }
- catch (string napis) {
- cout << napis << endl;
- }
- }
- void readFile()
- {
- try {
- if (file1) {
- const int LICZBA_ELEMENTOW = 1000;
- char bufor[LICZBA_ELEMENTOW];
- size_t odczytanychBajtow = fread(bufor, sizeof(char), LICZBA_ELEMENTOW, file1);
- cout << "\nZawartosc pliku " << name << ":" << endl << endl;
- for (int i = 0; i < (int)odczytanychBajtow; i++)
- cout << bufor[i];
- cout << endl << endl;
- }
- else {
- error = ("!!! Plik nie zostal otwarty !!!");
- throw error;
- }
- }
- catch (string napis) {
- cout << napis << endl;
- }
- }
- void writeFile()
- {
- try {
- if (!(file1)) {
- error = ("!!! Plik nie zostal otwarty !!!");
- throw error;
- }
- else {
- const int LICZBA_ELEMENTOW = 1000;
- char bufor[LICZBA_ELEMENTOW];
- cout << "Wpisz slowo, ktore ma sie znalezc w pliku: ";
- cin >> bufor;
- size_t zapisanychElementow = fwrite(bufor, sizeof(char), strlen(bufor), file1);
- cout << "Wyraz zapisano do pliku " << name << endl << endl;
- }
- }
- catch (string napis) {
- cout << napis << endl;
- }
- }
- ~LFile(){
- if (file1) {
- fclose(file1);
- number--;
- }
- };
- static int openFiles() {
- return number;
- }
- };
- int main()
- {
- cout << "Liczba otwartych plikow: " << LFile().openFiles() << endl;
- LFile plik1("pl1.txt"), plik2("pl2.txt"), plik3("pl3.txt"), plik4("pl4.txt"), plik5("pl5.txt"), plik6("pl6.txt");
- // plik1.readFile();
- // plik1.writeFile();
- // plik2.readFile();
- // plik2.writeFile();
- // plik3.readFile();
- // plik3.writeFile();
- // plik4.readFile();
- // plik4.writeFile();
- // plik5.readFile();
- // plik5.writeFile();
- // plik6.readFile();
- // plik6.writeFile();
- plik1.readFile();
- plik3.writeFile();
- /*plik1.~LFile();
- plik2.~LFile();
- plik3.~LFile();
- plik4.~LFile();
- plik5.~LFile();*/
- cout << "Liczba otwartych plikow: " << LFile().openFiles() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement