-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileOption.cpp
71 lines (60 loc) · 1.52 KB
/
FileOption.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
#include"FileOption.h"
using namespace std;
FileReader::FileReader() {
fp = NULL;
memset(buffer, 0x00, BUFFER_SIZE);
}
bool FileReader::openFile(char* fileLoca, char* readMode) {
this->fp = fopen(fileLoca, readMode);
if (this->fp == NULL)
return false;
else
return true;
}
bool FileReader::getline(char* data) {
//当文件打开失败时使用读取直接返回空
if (fp == NULL) return false;
else {
if (!feof(fp)) {
fgets(this->buffer,BUFFER_SIZE,this->fp);
if(strlen(this->buffer)==0)
return false;
if(this->buffer[strlen(this->buffer) - 1] == '\n')
this->buffer[strlen(this->buffer)-1] = '\0';
memcpy(data, this->buffer, sizeof(this->buffer)/sizeof(char));
memset(this->buffer, 0x00, BUFFER_SIZE);
return true;
}
else return false;
}
}
FileReader::~FileReader() {
if (this->fp != NULL)
fclose(fp);
}
FileWriter::FileWriter() {
this->fp = NULL;
}
bool FileWriter::openFile(char* fileLoca, char* readMode) {
this->fp = fopen(fileLoca, readMode);
if (this->fp == NULL)
return false;
else
return true;
}
bool FileWriter::putline(char* data) {
//putlien,效果类似于println,对于data 存在要求,使用\n 或者\0标记末尾
if (this->fp == NULL) return false;
else {
int putResult = 0;
putResult = fputs(data, this->fp);
if (putResult == EOF) return false;
putResult = fputs("\n", this->fp);
if (putResult == EOF) return false;
}
return true;
}
FileWriter::~FileWriter() {
if (this->fp == NULL)
fclose(this->fp);
}