-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileManager.cpp
More file actions
162 lines (129 loc) · 3.6 KB
/
FileManager.cpp
File metadata and controls
162 lines (129 loc) · 3.6 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
154
155
156
157
158
159
160
161
162
#include "stdafx.h"
#include "FileManager.h"
#include "ShapeHandler.h"
CFileManager* CFileManager::instance = NULL;
CFileManager* CFileManager::GetInstance()
{
if (!instance)
{
instance = new CFileManager();
}
return instance;
}
CFileManager::CFileManager() { }
CFileManager::~CFileManager() { SAFE_DELETE(instance); }
//////////////////////////////////////////////////////////////////////////
// JSon 이용
void CFileManager::SaveAsJson()
{
}
void CFileManager::LoadAsJson()
{
}
//////////////////////////////////////////////////////////////////////////
// csv 이용
bool CFileManager::SaveAsCsv()
{
CShapeHandler *CShapeHandler = CShapeHandler::GetInstance();
CString strPath = _T("C:\\Users\\kjh0121\\source\\repos\\Project1\\PhoneBookGUI\\media\\마이다스주소록_20180214.csv");
CString strStatus;
//파일 열기
ofstream file(strPath);
#pragma warning(push)
#pragma warning(disable: 4018)
//파일 쓰기
for (int i = 0; i < CShapeHandler->m_CaShape.size(); i++)
{
if (CShapeHandler->m_CaShape.at(i) == nullptr)
{
continue;
}
// file에 쓰고 싶은 내용 쓰기 !! 추가
// file << CStringToString(CShapeHandler->m_CaShape.at(i)->GetName()) << ',' << CStringToString(CShapeHandler->m_CaShape.at(i)->GetPhoneNumber())
// << ',' << CStringToString(CShapeHandler->m_CaShape.at(i)->GetTitle()) << ',' << CStringToString(CShapeHandler->m_CaShape.at(i)->GetJob());
file << '\n';
}
#pragma warning(pop)
if (file.fail()) //만약 bad() 함수가 실패 하면..
{
return FALSE;
}
file.close(); //파일 입출력 완료 후 닫아준다.
return TRUE;
}
bool CFileManager::LoadAsCsv()
{
CShapeHandler *CShapeHandler = CShapeHandler::GetInstance();
CString strPath = _T("C:\\Users\\kjh0121\\source\\repos\\Project1\\PhoneBookGUI\\media\\마이다스주소록_20180214.csv");
CString strName;
CString strPhoneNumber;
CString strTitle;
CString strJob;
//ifstream은 파일을 읽게 해주는 함수로써 ifstream (파일명 or 경로)
ifstream file(strPath);
if (file.fail()) //만약 bad() 함수가 실패 하면..
{
return FALSE;
}
CShapeHandler->m_CaShape.clear();
while (file.good()) //eof, bad, fail 함수가 거짓을 반환할 때까지
{
vector<string> row = CsvReadRow(file, ',');
if (!row[0].find("#")) //만약 csv 파일 안에 # 문자가 있을경우
{
continue; //그냥 건너 뛰어라
}
else //#문자가 없을 경우
{
// Add Shape!! 추가
// strName = StringToCString(row[0]);
// strJob = StringToCString(row[1]);
// strTitle = StringToCString(row[2]);
// strPhoneNumber = StringToCString(row[3]);
//CShapeHandler->m_CaShape.push_back(new Person(CShapeHandler->, strName, strPhoneNumber, strTitle, strJob));
}
}
file.close(); //파일 입출력 완료 후 닫아준다.
return TRUE;
}
vector<string> CFileManager::CsvReadRow(istream &file, char cDelimiter)
{
stringstream StringStream;
bool bInquotes = false; // 인용구, 큰 따옴표 같은 것 제거
vector<string> row; //relying on RVO
while (file.good())
{
char cGetFileChar = file.get();
if (!bInquotes && cGetFileChar == '"')
{
bInquotes = true;
}
else if (bInquotes && cGetFileChar == '"')
{
if (file.peek() == '"')
{
StringStream << (char)file.get();
}
else
{
bInquotes = false;
}
}
else if (!bInquotes && cGetFileChar == cDelimiter) // split 문자 만나면 stream 데이터를 벡터에 넣고 초기화
{
row.push_back(StringStream.str());
StringStream.str("");
}
else if (!bInquotes && (cGetFileChar == '\r' || cGetFileChar == '\n')) // 다음 줄을 만나도 stream 데이터를 벡터에 넣고 초기화
{
if (file.peek() == '\n') { file.get(); }
row.push_back(StringStream.str());
return row;
}
else
{
StringStream << cGetFileChar;
}
}
return row;
}