-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.cpp
More file actions
executable file
·153 lines (138 loc) · 3.64 KB
/
File.cpp
File metadata and controls
executable file
·153 lines (138 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* File.cpp
*
* Created on: 12 áàôø× 2018
* Author: Omer
*/
#include "File.h"
File::~File() {
if(--value->refCount==0)
delete value;
}
File& File::operator =(const File& otherFile) {
if(value == otherFile.value)
return *this;
if(--value->refCount == 0)
delete value;
value = otherFile.value;
++value->refCount;
return *this;
}
/*
* read char from text file in position i given.
*/
const char File::operator [](fstream::pos_type i) const throw(OutOfLimitException){
if(i > value->fileLength || i<0)
throw OutOfLimitException();
char c;
value->file->open(value->fileName, ios::in);
value->file->seekg(i);
value->file->read(&c,1);
value->file->close();
return c;
}
/*
* write char to file in a specific position given.
*/
void File::operator [](pair<size_t,char> posVal) {
char text[value->fileLength];
if(posVal.first > value->fileLength)
throw OutOfLimitException();
if(posVal.first<value->fileLength) //update already exists char
{
value->file->open(value->fileName, ios::in);
value->file->seekg(0,ios::beg);
value->file->read(text,value->fileLength); //read file to temorary string
value->file->close();
value->file->open(value->fileName, ios::out);
text[posVal.first]=posVal.second; //update string
value->file->write(text, value->fileLength); //write it back to text file
}
else
{
value->file->open(value->fileName, ios::app); //append char to the end of file.
*value->file << posVal.second;
value->file->write(posVal.second.c_str(), 1);
value->fileLength++; //update file length.
}
value->file->close();
}
/*
* Function update time signeture to the text file
*/
void File::touch(){
value->file->open(value->fileName); //update time signeture.
value->file->close();
}
/*
* function copy an exsisting file to other file name given.
*/
File* File::copy(string targetFile) throw(WriteToFileException){
fstream* ofs = new fstream(targetFile, ios::out);
char buff[value->fileLength];
value->file->open(value->fileName, ios::in);
value->file->seekg(0);
value->file->read(buff,value->fileLength); //read text from file
value->file->close();
ofs->write(buff, value->fileLength); //write text to target file
File* nwFile = new File(targetFile,*ofs); //create new files
ofs->close();
return nwFile;
}
/*
* function remove file.
*/
void File::remove() {
delete this;
}
/*
* function move file to other location by copy file text
* and delete the currant.
*/
bool File::move(string targetFile) {
copy(targetFile);
remove();
return true;
}
/*
* function display content of text file to the console
*/
void File::cat() const {
string line;
value->file->seekg(0);
value->file->open(value->fileName, ios::in);
while(getline(*(value->file),line)) //print line by line from text
cout << line << endl;
value->file->close();
}
/*
* function count how many lines, words and chars
* in text file and print it.
*/
void File::wc() const {
int lineCounter=0;
int wordCounter=0;
string line;
value->file->open(value->fileName,ios::in);
value->file->seekg(0);
while(getline(*(value->file),line))
{
for(size_t i=0;i<line.length();i++)
{
if(isspace(line[i]))
wordCounter++;
}
wordCounter++;
lineCounter++;
}
cout << lineCounter <<" "<<wordCounter<<" "<< value->fileLength<<" "<< value->fileName << endl;
value->file->close();
}
/*
* function create hard link to target file
*/
File* File::ln(string targetFile) const {
File* nwFile = new File(*this);
nwFile->setMyFileName(targetFile); //the only difference between the files.
return nwFile;
}