-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcandidate.cpp
78 lines (70 loc) · 1.82 KB
/
candidate.cpp
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
#include "candidate.h"
#include <errno.h>
Candidate::Candidate(string n,int id){
name = n;
code = id;
voteNumber = 0;
}
bool Candidate::saveCandidate(){
string path = "./DB/candidates/";
ostringstream convert;
convert<<code;
path+=convert.str();
cerr << "path is :"<< path << endl;
int fd = open(path.c_str(),O_WRONLY|O_RDONLY|O_CREAT);
if(-1 == fd)
{
printf("\n open() failed with error [%s]\n",strerror(errno));
return 1;
}
else
{
chmod(path.c_str(), S_IRUSR | S_IWUSR );
write(fd,name.c_str() ,strlen(name.c_str()));
write(fd,"\n",sizeof("\n"));
close(fd);
ofstream file(path.c_str());
if(file.is_open()){
file <<name << endl;
file << voteNumber << endl;
file.close();
}
else{
cerr << "Error in saving candidate " << name << endl;
return false;
}
cout<< "candidate "<< name << " added successfully!" << endl;
}
int fd_to_save_cname = open("./DB/candidates_names.txt", O_APPEND | O_RDWR);
cerr<<"uname is: "<<name<< " in " <<name.length()<<endl;
char* cstr = new char [name.length()+1];
strcpy(cstr, name.c_str());
int dumm = write(fd_to_save_cname, cstr, strlen(cstr));
dumm = write(fd_to_save_cname, "\n", strlen("\n"));
if(dumm <0)
write(STDOUTFD, "Error while writing port\n", sizeof("Error while writing port\n"));
close(fd_to_save_cname);
/*if(file_exist(path.c_str()))
return false;
ofstream file(path.c_str());
if(file.is_open()){
file <<name << endl;
file << voteNumber << endl;
file.close();
cerr << "candidate "<< name <<" saved successfully!" << endl;
}
else{
cerr << "Error in saving candidate " << name << endl;
return false;
}*/
return true;
}
void Candidate::setVoteNumber(int d){
voteNumber = d;
}
void Candidate::setCode(int i){
code = i;
}
void Candidate::setName(string n){
name =n;
}