我用的是vs2017,老師上課要求按書上的內容做題目,就要求通過ifstream和ofstream對MFC中的串列實作檔案的輸入輸出以及其他的要求,檔案編譯成功了但是沒有辦法創建txt檔案并讀寫這個檔案。代碼是按照書上打的,有點長,請大佬幫忙看看是怎么回事,謝謝啦!
#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(); //表明向對話框添加的訊息回應型別
}



uj5u.com熱心網友回復:
參考
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;
}
uj5u.com熱心網友回復:
聯系我
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/68665.html
標籤:基礎類
