-
Notifications
You must be signed in to change notification settings - Fork 2
/
TxtFile.cpp
79 lines (69 loc) · 1.71 KB
/
TxtFile.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
// TxtFile.cpp: implementation of the CTxtFile class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "TxtFile.h"
#include <shlwapi.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
BOOL CTxtFile::Open(LPCTSTR path, CFileException& fx)
{
if (PathFileExists(path))
{
if (ftxt_Std.Open(path, CFile::modeRead | CFile::typeText, &fx))
return TRUE; //成功入侵取得資料
else
{
errorMsg(fx);
ftxt_Std.Close();
return FALSE; //失敗
}
}
else
return FALSE;
}
void CTxtFile::file2mem()
{
dtxt_Txt.clear();
CString strTemp;
while (ftxt_Std.ReadString(strTemp))
{
strTemp.Format(_T("%s\n"), strTemp);
dtxt_Txt.push_back(strTemp);
}
}
BOOL CTxtFile::Save(LPCTSTR path, CFileException& fx)
{
if (ftxt_Std.Open(path, CFile::modeCreate | CFile::modeWrite | CFile::typeText, &fx))
return TRUE;
else
{
errorMsg(fx);
ftxt_Std.Close();
return FALSE;
}
}
void CTxtFile::mem2file()
{
if (!dtxt_Txt.empty())
{
for (TxtStrData::iterator it = dtxt_Txt.begin(); it != dtxt_Txt.end(); ++it)
//AfxMessageBox(*it);
ftxt_Std.WriteString(*it);
}
}
void CTxtFile::errorMsg(CFileException& fx)
{
//例外處理
TCHAR buf[255];
fx.GetErrorMessage(buf, 255);
CString strPrompt;
strPrompt.Format("CTxtFile說:「%s」", buf);
AfxMessageBox(strPrompt);
}