-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwcheck.cpp
More file actions
118 lines (115 loc) · 2.34 KB
/
Copy pathwcheck.cpp
File metadata and controls
118 lines (115 loc) · 2.34 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
#include <fstream>
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <dirent.h>
using namespace std;
int l=0;
#define a textfile[l]
#define b textfile[l+1]
#define c textfile[l+2]
#define d textfile[l+3]
#define e textfile[l+4]
int length;
vector<string> open(string path = ".")
{
DIR* dir;
dirent* pdir;
vector<string> files;
dir = opendir(path.c_str());
while (pdir = readdir(dir))
{
string fn = pdir->d_name;
if(fn.substr(fn.find_last_of(".") + 1) == "txt" && fn[0]=='w')
files.push_back(fn);
}
return files;
}
int main()
{
vector<string> files;
files = open(); // or pass which dir to open
fstream file;
for(int p=0;p<files.size();p++)
{
string temp = files[p];
file.open(temp.c_str(),fstream::in|fstream::out);
cout<<temp<<" is open"<<endl;
cout<<file.good()<<endl;
if (file.good())
{
//cout<<"if file"<<endl;
// get length of file:
l=0;
length=0;
file.seekg (0, file.end);
length = file.tellg();
file.seekg (0, file.beg);
char * buffer = new char [length];
//cout << "Reading " << length << " characters... ";
// read data as a block:
file.read (buffer,length);
string textfile(buffer);
//cout<<textfile<<endl;
string tname;
string line;
fstream tfile;
tfile.open("temp4.txt",fstream::in|fstream::out|fstream::trunc);
vector<string> lines;
int nooflines=0;
for(;l<length;l++)
{
if(a=='\n')
{
line+="\n";
lines.push_back(line);nooflines++;
line="";
}
else if(a==' ')
{
cout<<"extra space encountered"<<endl;
}
else
{
line+=textfile[l];
}
if(l>length)
{
break;
}
}
for(int q=0;q<nooflines;q++)
{
if(lines[q].size()<2)
{
cout<<"encountered extra new line"<<endl;
}
else
{
tfile<<lines[q];
}
}
tfile.close();
remove(temp.c_str());
if(rename("temp4.txt",temp.c_str()))
cout<<"File is renamed as : "<<tname<<endl;
else
{
perror( "Error renaming file" );
}
delete[] buffer;
cout<<endl;
textfile="";
}
else
{
cout<<"End of file reached for some reason"<<endl;
}
file.close();
}
remove("temp4.txt");
return 0;
}