16,471
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include "List6005.h"
#include "List6005Dlg.h"
#include "afxdialogex.h"
#include <fstream>//即ifstream和ofstream的头文件,在使用的时候必须加上命名空间
#include <vector>
#include <string>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
using std::vector;
using std::string;
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CList6005Dlg 对话框
CList6005Dlg::CList6005Dlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_LIST6005_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CList6005Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_List);//关联成员变量
DDX_Control(pDX, IDC_STATIC1, m_CityName);
DDX_Control(pDX, IDC_STATIC2, m_CityInfo);
}
BEGIN_MESSAGE_MAP(CList6005Dlg, CDialogEx)//消息映射
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_LBN_SELCHANGE(IDC_LIST1, &CList6005Dlg::OnSelchangeList16005)
ON_BN_CLICKED(IDC_BUTTON2, &CList6005Dlg::OnClickedButton60052)
ON_BN_CLICKED(IDC_BUTTON1, &CList6005Dlg::OnClickedBuDelete6005)
ON_BN_CLICKED(IDC_BUTTON3, &CList6005Dlg::OnClickedBuReset)
ON_COMMAND(IDD_LIST6005_DIALOG, &CList6005Dlg::OnClose)
END_MESSAGE_MAP()
// CList6005Dlg 消息处理程序
BOOL CList6005Dlg::OnInitDialog() //初始化
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
ifstream in("G:\\txt\\city.txt"); //创建文件输入流
string city_name, city_info;
while (in >> city_name >> city_info) //读取文件中的城市名称和信息
{
city.push_back(city_name); //将城市名称添加至city中,下一行一样
city.push_back(city_info);
m_List.AddString(city_name.c_str()); //将城市名称添加至列表框中
}
in.close(); //关闭文件流
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CList6005Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CList6005Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CList6005Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CList6005Dlg::OnSelchangeList16005()
{
// TODO: 在此添加控件通知处理程序代码
vector<string>::size_type t = m_List.GetCurSel(); //获取当前项下标
m_CityName.SetWindowText(city[t].c_str()); //设置显示城市名称
m_CityInfo.SetWindowText(info[t].c_str()); //设置显示城市信息
}
void CList6005Dlg::OnClickedButton60052()
{
// TODO: 在此添加控件通知处理程序代码
CString city_name, city_info;
GetDlgItem(IDC_EDIT1)->GetWindowText(city_name); //获取城市名称
GetDlgItem(IDC_EDIT2)->GetWindowText(city_info);
if (city_name.IsEmpty() || city_info.IsEmpty()) //判断若字符串为空
{
MessageBox(_T("请填写完整信息", "信息不完整", MB_OK));
}
else
{
int count = m_List.GetCount(); //获取列表框中项数
for (int i = 0; i != count; ++i)
{
CString str;
m_List.GetText(i, str); //获取列表项的文本
if (city_name == str) //判断输入城市名称是否相同
{
MessageBox(_T("该城市信息已存在", "数据重复", MB_OK));
return;
}
}
city.push_back(city_name.GetBuffer()); //将城市信息加入向量中
info.push_back(city_info.GetBuffer());
m_List.AddString(city_name); //将城市名称加入到列表框中
GetDlgItem(IDC_EDIT1)->SetWindowText(""); //清空编辑框
GetDlgItem(IDC_EDIT2)->SetWindowText("");
}
}
void CList6005Dlg::OnClickedBuDelete6005()
{
// TODO: 在此添加控件通知处理程序代码
int count = m_List.GetCurSel(); //获取删除项下标
vector<string>::iterator ivec1 = city.begin();
vector<string>::iterator ivec2 = info.begin();
ivec1 += count;
ivec2 += count;
city.erase(ivec1); //在向量中删除城市
info.erase(ivec2);
m_List.DeleteString(count); //在列表中删除城市名称
m_CityName.SetWindowText("");
m_CityInfo.SetWindowText("");
}
void CList6005Dlg::OnClickedBuReset()
{
// TODO: 在此添加控件通知处理程序代码
GetDlgItem(IDC_EDIT1)->SetWindowText("");
GetDlgItem(IDC_EDIT2)->SetWindowText("");
}
void CList6005Dlg::OnClose()
{
// TODO: 在此添加命令处理程序代码
ofstream out("G:\\txt\\city.txt"); //创建输出文件流
vector<string>::size_type t = city.size(); //获取向量长度
for (vector<string>::size_type i = 0; i != t; ++i)
{
out << city[i] << "\t" << info[i] << endl; //将城市信息写入文件
}
out.close(); //关闭文件流
CDialogEx::OnClose(); //表明向对话框添加的消息响应类型
}
vector<string> lines_of_text;
int main(int argc,char* argv[])
{
string file_name;
file_name="新建文本文档.txt";
ifstream infile(file_name.c_str(),ios::in);
if(!infile)
{
cerr<<"oops! unable to open file "<<file_name<<" -- bailing out!\n";
exit(-1);
}
string textline;
while(getline(infile,textline,'\n'))
{
lines_of_text.push_back(textline);
}
char *file="aa.txt";
ofstream fout(file,ios_base::out);
vector<string>::iterator itr;
for (itr = lines_of_text.begin(); itr != lines_of_text.end(); ++ itr)
{
cout << *itr <<'\n';
fout << *itr << endl;
}
system("pause");
return 0;
}